<?php

namespace App\Api\Controllers\Merchant;

use Alipayopen\Sdk\AopClient;
use Alipayopen\Sdk\Request\AlipayTradeQueryRequest;
use Alipayopen\Sdk\Request\AlipayTradeRefundRequest;
use App\Api\Controllers\BaseController;
use App\Api\Controllers\Config\CoolConfigController;
use App\Api\Controllers\Config\AlipayIsvConfigController;
use App\Api\Controllers\Config\ChangshaConfigController;
use App\Api\Controllers\Config\PinganConfigController;
use App\Api\Controllers\Config\EasyPayConfigController;
use App\Api\Controllers\Config\HConfigController;
use App\Api\Controllers\Config\CcbConfigController;
use App\Api\Controllers\Config\HkrtConfigController;
use App\Api\Controllers\Config\MerchConfigController;
use App\Api\Controllers\Config\HuiPayConfigController;
use App\Api\Controllers\Config\HwcPayConfigController;
use App\Api\Controllers\Config\JdConfigController;
use App\Api\Controllers\Config\NongjinConfigController;
use App\Api\Controllers\Config\SaobeiConfigController;
use App\Api\Controllers\Config\WingConfigController;
use App\Api\Controllers\Config\AbcConfigController;
use App\Api\Controllers\Config\LianfuConfigController;
use App\Api\Controllers\Config\LianfuyoupayConfigController;
use App\Api\Controllers\Config\LinkageConfigController;
use App\Api\Controllers\Config\LtfConfigController;
use App\Api\Controllers\Config\MyBankConfigController;
use App\Api\Controllers\Config\NewLandConfigController;
use App\Api\Controllers\Config\SuzhouConfigController;
use App\Api\Controllers\Config\TfConfigController;
use App\Api\Controllers\Config\VbillConfigController;
use App\Api\Controllers\Config\WeixinConfigController;
use App\Api\Controllers\Config\WftPayConfigController;
use App\Api\Controllers\Config\YinshengConfigController;
use App\Api\Controllers\DuoLaBao\ManageController;
use App\Api\Controllers\MyBank\TradePayController;
use App\Api\Controllers\Newland\PayController;
use App\Api\Controllers\HwcPay\PayController as HwcPayPayController;
use App\Api\Controllers\WftPay\PayController as WftPayPayController;
use App\Api\Controllers\YinSheng\PayController as YinShengPayController;
use App\Api\V2\Controller\Order\Merchant\OrderQueryController;
use App\Common\PaySuccessAction;
use App\Common\RefundSuccessAction;
use App\Common\StoreDayMonthOrder;
use App\Common\UserGetMoney;
use App\Models\AlipayAppOauthUsers;
use App\Models\AlipayHbOrder;
use App\Models\AlipayIsvConfig;
use App\Models\MerchantFuwu;
use App\Models\MerchantStore;
use App\Models\MerchantStoreDayOrder;
use App\Models\MerchantStoreMonthOrder;
use App\Models\MerchantWalletDetail;
use App\Models\MyBankConfig;
use App\Models\MyBankStore;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\RefundOrder;
use App\Models\RefundOrderDoing;
use App\Models\Store;
use App\Models\StoreDayOrder;
use App\Models\StoreMonthOrder;
use App\Models\UserWalletDetail;
use App\Services\Order\MerchantService;
use App\Services\Pay\GuoTonXinYi\BizService;
use App\Util\LockUtil;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use App\Api\Controllers\Config\JiuJiangConfigController;

class OrderController extends BaseController{

    // public function order(Request $request){
    //     try {
    //         $merchant = $this->parseToken();
    //         $store_id = $request->get('store_id', '');
    //         $merchant_id = $request->get('merchant_id', '');
    //         $pay_status = $request->get('pay_status', '');
    //         $discount = $request->get('discount', '');
    //         $ways_source = $request->get('ways_source', '');
    //         $ways_type = $request->get('ways_type', '');
    //         $time_start_s = date('Y-m-d 00:00:00', time());
    //         $time_start_e = date('Y-m-d 23:59:59', time());
    //         $company = $request->get('company', '');

    //         $time_start = $request->get('time_start', $time_start_s);
    //         $time_end = $request->get('time_end', $time_start_e);

    //         $out_trade_no = $request->get('out_trade_no', '');
    //         $device_id = $request->get('device_id', '');

    //         $trade_no = $request->get('trade_no', '');
    //         $return_type = $request->get('return_type', '1');

    //         //限制时间
    //         $date = (strtotime($time_end) - strtotime($time_start)) / 86400;
    //         $day = 31;
    //         $now_time = date('Y-m-d H:i:s', time());
    //         $end = date('Y-m-d 22:00:00', time());
    //         if ($now_time > $end) {
    //             $day = 31;
    //         }

    //         if ($date > $day) {
    //             $time_start_s = date('Y-m-d 00:00:00', time());
    //             $time_start_e = date('Y-m-d 23:59:59', time());
    //             $time_start = $time_start_s;
    //             $time_end = $time_start_e;
    //         }
    //         // print_r($time_end);die;

    //         //跨天操作
    //         $time_start_db = date('Ymd', strtotime($time_start));
    //         $time_end_db = date('Ymd', strtotime($time_end));
    //         $is_ct_time = 0;
    //         if ($time_start_db != $time_end_db) {
    //             $is_ct_time = 1;
    //         }

    //         $sort = $request->get('sort', '');

    //         $day = date('Ymd', strtotime($time_end));
    //         $table = 'orders_' . $day;

    //         if (env('DB_D1_HOST')) {
    //             //有没有跨天
    //             if ($is_ct_time) {
    //                 if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
    //                     $obj = DB::connection("mysql_d1")->table('order_items');
    //                 } elseif (Schema::hasTable('orders2020') && $time_start < "2021-01-01 00:00:00") {
    //                     $obj = DB::connection("mysql_d1")->table('orders2020');
    //                 } else {
    //                     $obj = DB::connection("mysql_d1")->table('orders');
    //                 }
    //             } else {
    //                 if (Schema::hasTable($table)) {
    //                     $obj = DB::connection("mysql_d1")->table($table);
    //                 } else {
    //                     if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
    //                         $obj = DB::connection("mysql_d1")->table('order_items');
    //                     } elseif (Schema::hasTable('orders2020') && $time_start < "2021-01-01 00:00:00") {
    //                         $obj = DB::connection("mysql_d1")->table('orders2020');
    //                     } else {
    //                         $obj = DB::connection("mysql_d1")->table('orders');
    //                     }
    //                 }
    //             }
    //         } else {
    //             if ($is_ct_time) {
    //                 if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
    //                     $obj = DB::table('order_items');
    //                 } elseif (Schema::hasTable('orders2020') && $time_start < "2021-01-01 00:00:00") {
    //                     $obj = DB::table('orders2020');
    //                 } else {
    //                     $obj = DB::table('orders');
    //                 }
    //             } else {
    //                 if (Schema::hasTable($table)) {
    //                     $obj = DB::table($table);
    //                 } else {
    //                     if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
    //                         $obj = DB::table('order_items');
    //                     } elseif (Schema::hasTable('orders2020') && $time_start < "2021-01-01 00:00:00") {
    //                         $obj = DB::table('orders2020');
    //                     }  else {
    //                         $obj = DB::table('orders');
    //                     }
    //                 }
    //             }
    //         }

    //         $where = [];
    //         $store_ids = [];
    //         if ($out_trade_no) {
    //             $where[] = ['out_trade_no', 'like', $out_trade_no . '%'];
    //         }

    //         if ($trade_no) {
    //             $where[] = ['trade_no', 'like', $trade_no . '%'];
    //         }
    //         //收银员
    //         if ($merchant->merchant_type == 2) {
    //             $where[] = ['merchant_id', '=', '' . $merchant->merchant_id . ''];
    //         }

    //         //是否传收银员ID
    //         if ($merchant_id && $merchant_id != "NULL") {
    //             $where[] = ['merchant_id', '=', '' . $merchant_id . ''];
    //         }

    //         if ($pay_status) {
    //             $where[] = ['pay_status', '=', $pay_status];
    //         }

    //         if ($discount==1) {
    //             $where[] = ['discount_amount', '>', 0];
    //         }

    //         if ($device_id) {
    //             $where[] = ['device_id', '=', $device_id];
    //         }

    //         if ($store_id && $store_id != "NULL") {
    //             $store_ids = [
    //                 [
    //                     'store_id' => $store_id,
    //                 ]
    //             ];
    //         } else {
    //             $MerchantStore = MerchantStore::where('merchant_id', $merchant->merchant_id)
    //                 ->select('store_id')
    //                 ->get();

    //             if (!$MerchantStore->isEmpty()) {
    //                 $store_ids = $MerchantStore->toArray();
    //             }
    //         }

    //         if ($company) {
    //             $where[] = ['company', '=', $company];
    //         }

    //         if ($ways_source) {
    //             if (in_array($ways_source, ['alipay_face', 'weixin_face'])) {
    //                 $where[] = ['pay_method', '=', $ways_source];

    //             } else {
    //                 $where[] = ['ways_source', '=', $ways_source];
    //             }
    //         }

    //         if ($ways_type) {
    //             $where[] = ['ways_type', '=', $ways_type];
    //         }

    //         if ($time_start) {
    //             $time_start = date('Y-m-d H:i:s', strtotime($time_start));
    //             $where[] = ['created_at', '>=', $time_start];
    //         }

    //         if ($time_end) {
    //             $time_end = date('Y-m-d H:i:s', strtotime($time_end));
    //             $where[] = ['created_at', '<=', $time_end];
    //         }
    //         // print_r($where);die;

    //         if ($sort) {
    //             //返回基础数据
    //             if ($return_type == '1') {
    //                 $obj = $obj->where($where)
    //                     ->whereIn('store_id', $store_ids)
    //                     ->orderBy('total_amount', $sort);
    //             } else {
    //                 $obj = $obj->where($where)
    //                     ->whereIn('store_id', $store_ids)
    //                     ->orderBy('total_amount', $sort);
    //             }

    //         } else {
    //             if ($return_type == '1') {
    //                 //返回基础数据
    //                 $obj = $obj->where($where)
    //                     ->whereIn('store_id', $store_ids)
    //                     ->orderBy('created_at', 'desc');
    //             } else {
    //                 $obj = $obj->where($where)
    //                     ->whereIn('store_id', $store_ids)
    //                     ->orderBy('created_at', 'desc');
    //             }
    //         }
    //         // print_r( $store_ids );die;

    //         $this->t = $obj->count();
    //         $data = $this->page($obj)->get();
    //         $data=json_decode($data,true);
    //         foreach ($data as $k=>$v){
    //             $data[$k]['discount_amount']=sprintf("%.2f",$v['discount_amount']);
    //             $data[$k]['actual_amount']=sprintf("%.2f",$v['total_amount']);
    //         }
    //         $this->status = 1;
    //         $this->message = '数据返回成功';

    //         return $this->format($data);
    //     } catch (\Exception $exception) {
    //         $this->status = -1;
    //         $this->message = $exception->getMessage() . $exception->getLine();
    //         return $this->format();
    //     }
    // }

    public function orderNew(Request $request){
        try {
            $merchant = $this->parseToken();
            if (4776 == $merchant->merchant_id) {
                die;
            }
            /**
            $store_id = $request->get('store_id', '');
            $merchant_id = $request->get('merchant_id', '');
            $pay_status = $request->get('pay_status', '');
            $discount = $request->get('discount', '');
            $ways_source = $request->get('ways_source', '');
            $ways_type = $request->get('ways_type', '');
            $time_start_s = date('Y-m-d 00:00:00', time());
            $time_start_e = date('Y-m-d 23:59:59', time());
            $company = $request->get('company', '');

            $time_start = $request->get('time_start', $time_start_s);
            $time_end = $request->get('time_end', $time_start_e);

            $out_trade_no = $request->get('out_trade_no', '');
            $device_id = $request->get('device_id', '');

            $trade_no = $request->get('trade_no', '');
            $return_type = $request->get('return_type', '1');
             */
            $orderQueryController=new OrderQueryController();
            $where=[
                "page"=>$request->input("p",1),
                "pagesize"=>$request->input("l",200),
                "store_id"=>$request->input("store_id",""),
                "merchant_id"=> $request->input('merchant_id', ''),
                "pay_status"=>$request->input("pay_status",""),
                "discount" => $request->input('discount', ''),
                "ways_source"=>$request->input("ways_source",""),
                "ways_type"=>$request->input("ways_type",""),
                "company"=>$request->input("company",""),
                "device_id"=>$request->input("device_id",""),
                "out_trade_no"=>$request->input("out_trade_no",""),
                "trade_no"=>$request->input("trade_no",""),
                "start_date"=>$request->get('time_start', ''),
                "end_date"=>$request->get('time_start', ''),
            ];
            $where=$orderQueryController->returnWhere($merchant,$where);
            $service = new MerchantService();
//            PrintSqlService::listen();
            $result = $service->orderQuery($where);
            $this->needpage=true;
            $this->l = $where["pagesize"];
            $this->p = $where["page"];
            $this->t = $result["total"];
            $this->status = 1;
            $this->message = '数据返回成功';
            $data=$result["list"];
            foreach ($data as $k=>$v){
                $v['discount_amount']=sprintf("%.2f",$v['discount_amount']);
                $v['actual_amount']=sprintf("%.2f",$v['total_amount']-$v['discount_amount']);
                $data[$k]=$v;
            }
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = '暂无数据'.$exception->getMessage();
            return $this->format();
        }
    }


    public function order(Request $request){
        try {
            $merchant = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $merchant_id = $request->get('merchant_id', '');
            $pay_status = $request->get('pay_status', '');
            $discount = $request->get('discount', '');
            $ways_source = $request->get('ways_source', '');
            $ways_type = $request->get('ways_type', '');
            $time_start_s = date('Y-m-d 00:00:00', time());
            $time_start_e = date('Y-m-d 23:59:59', time());
            $company = $request->get('company', '');

            $time_start = $request->get('time_start', $time_start_s);
            $time_end = $request->get('time_end', $time_start_e);

            $out_trade_no = $request->get('out_trade_no', '');
            $device_id = $request->get('device_id', '');

            $trade_no = $request->get('trade_no', '');
            $return_type = $request->get('return_type', '1');

            //限制时间
            $date = (strtotime($time_end) - strtotime($time_start)) / 86400;
            $day = 31;
            $now_time = date('Y-m-d H:i:s', time());
            $end = date('Y-m-d 22:00:00', time());
            if ($now_time > $end) {
                $day = 31;
            }
            
            if( $time_end > date('Y-m-d 23:59:59', time()) ){ // 结束时间超过今天改为今天
                return json_encode([
                    'status' => 2,
                    'message' => '流水查询时间超过时间范围'
                ]);
                
            }

            if ($date > $day) {
                $time_start_s = date('Y-m-d 00:00:00', time());
                $time_start_e = date('Y-m-d 23:59:59', time());
                $time_start = $time_start_s;
                $time_end = $time_start_e;
            }
            if(!$time_end){
                $time_end = $time_start_e;
            }

            //跨天操作
            $time_start_db = date('Ymd', strtotime($time_start));
            $time_end_db = date('Ymd', strtotime($time_end));
            $is_ct_time = 0;
            if ($time_start_db != $time_end_db) {
                $is_ct_time = 1;
            }
            $sort = $request->get('sort', '');

            $day = date('Ymd', strtotime($time_end));
            $table = 'orders_' . $day;

            $where = [];
            $store_ids = [];
            if ($out_trade_no) {
                $where[] = ['out_trade_no', 'like', $out_trade_no . '%'];
            }

             $qwx_no = $request->get('qwx_no', '');
            if ($qwx_no) {
                $where[] = ['qwx_no', '=', $qwx_no];
            }
            
            if ($trade_no) {
                $where[] = ['trade_no', 'like', $trade_no . '%'];
            }
            //收银员
            if ($merchant->merchant_type == 2) {
                $where[] = ['merchant_id', '=', '' . $merchant->merchant_id . ''];
            }
            
            if (4776 == $merchant->merchant_id) {
                die;
            }
            

            //是否传收银员ID
            if ($merchant_id && $merchant_id != "NULL") {
                $where[] = ['merchant_id', '=', '' . $merchant_id . ''];
            }

            if ($pay_status) {
                $where[] = ['pay_status', '=', $pay_status];
            }

            if ($discount==1) {
                $where[] = ['discount_amount', '>', 0];
            }

            if ($device_id) {
                $where[] = ['device_id', '=', $device_id];
            }

            if ($store_id && $store_id != "NULL") {
                $where[] = ['store_id', '=', $store_id];
                $store_ids = [
                    [
                        'store_id' => $store_id,
                    ]
                ];
            } else {
                $MerchantStore = MerchantStore::where('merchant_id', $merchant->merchant_id)->select('store_id')->get();

                if (!$MerchantStore->isEmpty()) {
                    $store_ids = $MerchantStore->toArray();
                    $where_stores = '';
                    foreach( $store_ids as $k => $v ){
                        if($k == 0){
                            $where_stores = "'" . $v['store_id'] . "'";
                        }else{
                            $where_stores = $where_stores . ", '" .$v['store_id']. "'";
                        }
                    }
                    $where_store = [ 'store_id', ' in ', '('.$where_stores.')' ];
                }
            }

            if ($company) {
                $where[] = ['company', '=', $company];
            }

            if ($ways_source) {
                if (in_array($ways_source, ['alipay_face', 'weixin_face'])) {
                    $where[] = ['pay_method', '=', $ways_source];

                } else {
                    $where[] = ['ways_source', '=', $ways_source];
                }
            }

            if ($ways_type) {
                $where[] = ['ways_type', '=', $ways_type];
            }

            if ($time_start) {
                $time_start = date('Y-m-d H:i:s', strtotime($time_start));
                $where[] = ['created_at', '>=', $time_start];
            }
            
            if ($time_end) {
                $time_end = date('Y-m-d H:i:s', strtotime($time_end));
                $where[] = ['created_at', '<=', $time_end];
            }
            
            //根据交易时间判断
            $trading_time_start = $request->get('trading_time_start');
            $trading_time_end = $request->get('trading_time_end');
            if ($trading_time_end || $trading_time_start) {
                if ($trading_time_start) {
                    $trading_time_start = date('Y-m-d H:i:s', strtotime($trading_time_start));
                    $where[] = ['pay_time', '>=', $trading_time_start];
                } else {
                    $trading_time_start = date('Y-m-d 00:00:00');
                    $where[] = ['pay_time', '>=', date('Y-m-d 00:00:00')];
                }
                if ($trading_time_end) {
                    $trading_time_end = date('Y-m-d H:i:s', strtotime($trading_time_end));
                    $where[] = ['pay_time', '<=', $trading_time_end];
                } else {
                    $trading_time_end = date('Y-m-d 23:59:59');
                    $where[] = ['pay_time', '<=', date('Y-m-d 23:59:59')];
                }
                $tradingDiff = (strtotime($trading_time_end) - strtotime($trading_time_start)) / 86400;
                $newWhere = [];
                foreach ($where as  $wv) {
                    if (!in_array('created_at', $wv)) {
                        $newWhere[] = $wv;
                    }
                }
                $where = $newWhere;
                if ($tradingDiff > 31 || $tradingDiff < 0) {
                    return json_encode([
                        'status' => 2,
                        'message' => '交易时间大于31天或结束时间小于开始时间'
                    ]);
                }
            }

                    
            if( $time_start >= date('Y-m-d', time()) ) { // 今天
                $obj = DB::table('orders');
//                print_r( 2 );die;
            }else{
                // print_r( $is_ct_time );die;
                if( $is_ct_time ){
                    $time = $this->getDateFromRanges($time_start_db, $time_end_db);
                    $toEnd = count($time) - 1;
                    // print_r( $toEnd );die;

                    foreach( $time as $k => $v ){
                        
                        $where = [];
                        $store_ids = [];
                        if ($out_trade_no) {
                            $where[] = ['out_trade_no', 'like', $out_trade_no . '%'];
                        }
            
                        if ($trade_no) {
                            $where[] = ['trade_no', 'like', $trade_no . '%'];
                        }
                        //收银员
                        if ($merchant->merchant_type == 2) {
                            $where[] = ['merchant_id', '=', '' . $merchant->merchant_id . ''];
                        }
            
                        //是否传收银员ID
                        if ($merchant_id && $merchant_id != "NULL") {
                            $where[] = ['merchant_id', '=', '' . $merchant_id . ''];
                        }
            
                        if ($pay_status) {
                            $where[] = ['pay_status', '=', $pay_status];
                        }
            
                        if ($discount==1) {
                            $where[] = ['discount_amount', '>', 0];
                        }
            
                        if ($device_id) {
                            $where[] = ['device_id', '=', $device_id];
                        }
            
                        if ($store_id && $store_id != "NULL") {
                            $where[] = ['store_id', '=', $store_id];
                            $store_ids = [
                                [
                                    'store_id' => $store_id,
                                ]
                            ];
                        } else {
                            $MerchantStore = MerchantStore::where('merchant_id', $merchant->merchant_id)->select('store_id')->get();
            
                            if (!$MerchantStore->isEmpty()) {
                                $store_ids = $MerchantStore->toArray();
                                $where_stores = '';
                                foreach( $store_ids as $key => $val ){
                                    if($key == 0){
                                        $where_stores = "'" . $val['store_id'] . "'";
                                    }else{
                                        $where_stores = $where_stores . ", '" .$val['store_id']. "'";
                                    }
                                }
                                $where_store = [ 'store_id', ' in ', '('.$where_stores.')' ];
                            }
                        }
            
                        if ($company) {
                            $where[] = ['company', '=', $company];
                        }
            
                        if ($ways_source) {
                            if (in_array($ways_source, ['alipay_face', 'weixin_face'])) {
                                $where[] = ['pay_method', '=', $ways_source];
            
                            } else {
                                $where[] = ['ways_source', '=', $ways_source];
                            }
                        }
            
                        if ($ways_type) {
                            $where[] = ['ways_type', '=', $ways_type];
                        }
            
                        if ($time_start) {
                            if($k==0){
                                $where[] = ['created_at', '>=', $time_start];
                            }else{
                                $where[] = ['created_at', '>=', date('Y-m-d', strtotime($v)).' 00:00:00'];
                            }
                        }
                        
                        if ($time_end) {
                            if($k == $toEnd){
                                $where[] = ['created_at', '<=', $time_end];
                            }else{
                                $where[] = ['created_at', '<=', date('Y-m-d', strtotime($v)).' 23:59:59'];
                            }
                        }
                        if (!$store_id) {
                            $where[] = $where_store;
                        }
                        
                        // print_r( $where );die;
                        $wheres = '';
                        foreach ($where as $key => $val){
    //                        print_r( $v[0] );die;
                            if( $val[0] == 'store_id' ){
                                if($key == 0){
                                    $wheres = $wheres.' where `'.$val[0].'`'.$val[1].$val[2];
                                }else{
                                    $wheres = $wheres.' and `'.$val[0].'`'.$val[1].$val[2];
                                }
                            }else{
                                if($key == 0){
                                    $wheres = $wheres.' where `'.$val[0].'`'.$val[1]."'".$val[2]."'";
                                }else{
                                    $wheres = $wheres.' and `'.$val[0].'`'.$val[1]."'".$val[2]."'";
                                }
                            }
                        }
                    // print_r( $wheres );die;
                    
                        if( $v == date('Y-m-d', time()) ){ // 今天
                            $day_start = 'orders';
                        }else{//10377
                             $day_start = 'orders_' . date( 'Ymd', strtotime($v) );
                        }
                        if($k == 0){
                            $test = '(select * from `' . $day_start . '`' . $wheres . ')'; // 列表数据
                            $count = '(select count(*) from `' . $day_start . '`' . $wheres . ')'; // 总记录条数(分页)
                        }else{
                            $test = $test . ' union all ' . '(select * from `' . $day_start . '`' . $wheres . ')'; // 列表数据
                            $count = $count . ' union all ' . '(select count(*) from `' . $day_start . '`' . $wheres . ')'; // 总记录条数(分页)
                        }
                    }
                    $start = abs((abs(trim($request->get('p', 1))) - 1) * abs(trim($request->get('l', 25))));
                    $l = abs( trim($request->get('l', 25)) );
                    $test = $test . ' order by `created_at` desc limit ' . $start . "," . $l;

                    

                    $data = DB::select($test);
                    $count = DB::select($count);

                    $count = json_decode( json_encode($count, true), true );

                    $total_num = 0;
                    foreach($count as $k => $v){
                        $total_num = $total_num + $v['count(*)'];
                    }

                    $this->needpage = 1;
                    $this->t = $total_num;

                    foreach ($data as $k => $v){
                        $v=(array)$v;
                        $v['refund_time'] = $this->refundTime($v['out_trade_no']);
                        $data[$k]=$v;
                    }

                    $this->status = 1;
                    $this->message = '数据返回成功'.$merchant->merchant_id;

                    return $this->format($data);

                }else{
                    $day = date('Ymd', strtotime($time_start));
                    $table = 'orders_' . $day;

                    $obj = DB::table($table);
                }
            }



//            print_r( $where );die;

            if ($sort) {
                //返回基础数据
                if ($return_type == '1') {
                    $obj = $obj->where($where)
                        ->whereIn('store_id', $store_ids)
                        ->orderBy('total_amount', $sort);
                } else {
                    $obj = $obj->where($where)
                        ->whereIn('store_id', $store_ids)
                        ->orderBy('total_amount', $sort);
                }

            } else {
                if ($return_type == '1') {
                    //返回基础数据
                    $obj = $obj->where($where)
                        ->whereIn('store_id', $store_ids)
                        ->orderBy('created_at', 'desc');
                } else {
                    $obj = $obj->where($where)
                        ->whereIn('store_id', $store_ids)
                        ->orderBy('created_at', 'desc');
                }
            }

                    if ($merchant->merchant_id == 10377) {
                        $this->t = 1000;
                        $data = $this->page($obj)->get();
                        
                    } else {
                        $this->t = $obj->count();
                        $data = $this->page($obj)->get();
                    }


            //$this->t = $obj->count();
            //$data = $this->page($obj)->get();
            $data=json_decode($data,true);
            foreach ($data as $k=>$v){
                $data[$k]['discount_amount']=sprintf("%.2f",$v['discount_amount']);
                $data[$k]['actual_amount']=sprintf("%.2f",$v['total_amount']-$v['discount_amount']);
                $data[$k]['refund_time'] = $this->refundTime($v['out_trade_no']);
            }
            $this->status = 1;
            $this->message = '数据返回成功'.$merchant->merchant_id;

            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage() . $exception->getLine();
            return $this->format();
        }
    }

    public function refundTime ($out_trade_no)
    {
        return DB::table('refund_orders')->where(['out_trade_no' => $out_trade_no])->value('created_at');
    }


    public function order_info(Request $request)
    {
        try {
            $merchant = $this->parseToken();
            $out_trade_no = $request->get('out_trade_no', '');

            $a = array('a', '_', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
            $b = str_ireplace($a, "", $out_trade_no);
            $day = substr($b, 0, 8);
            $table = 'orders_' . $day;

            if (env('DB_D1_HOST')) {
                if (Schema::hasTable($table)) {
                    $data = DB::connection("mysql_d1")->table($table)->orWhere('out_trade_no', $out_trade_no)
                        ->orWhere('trade_no', $out_trade_no)
                        ->first();
                } else {
                    $data = DB::connection("mysql_d1")->table("orders")->orWhere('out_trade_no', $out_trade_no)
                        ->orWhere('trade_no', $out_trade_no)
                        ->first();
                }
            } else {
                if (Schema::hasTable($table)) {
                    $data = DB::table($table)->orWhere('out_trade_no', $out_trade_no)
                        ->orWhere('trade_no', $out_trade_no)
                        ->first();
                } else {
                    $data = DB::table('orders')
                        ->where('out_trade_no', $out_trade_no)
                        ->first();
                    if (!$data) {
                        $data = DB::table('orders')
                            ->where('trade_no', $out_trade_no)
                            ->first();
                        if (!$data) {
                            $data = DB::table('orders')
                                ->where('auth_code', $out_trade_no)
                                ->first();
                        }
                    }
                }
            }

            if (!$data && Schema::hasTable('orders2020')) {
                $data = DB::table('orders2020')
                    ->orWhere('out_trade_no', $out_trade_no)
                    ->first();
                if (!$data) {
                    $data = DB::table('orders2020')
                        ->where('trade_no', $out_trade_no)
                        ->first();
                    if (!$data) {
                        $data = DB::table('orders2020')
                            ->where('auth_code', $out_trade_no)
                            ->first();
                    }
                }
            }

            if (!$data) {
                return json_encode([
                    'status' => 2,
                    'message' => '订单号不存在'
                ]);
            }

            //保证订单号是此门店的
            $MerchantStore = MerchantStore::where('store_id', $data->store_id)
                ->where('merchant_id', $merchant->merchant_id)
                ->select('id')
                ->first();
            if (!$MerchantStore) {
                return json_encode([
                    'status' => 2,
                    'message' => '订单号不在你的查询范围'
                ]);
            }

            try {
                if ($data->pay_status == 2) {
                    $order_foreach_public = [
                        'out_trade_no' => $data->out_trade_no,
                        'store_id' => $data->store_id,
                        'ways_type' => $data->ways_type,
                        'config_id' => $data->config_id,
                        'table' => $table,
                    ];
                    $obj = new OrderController();
                    $obj->order_foreach_public($order_foreach_public);
                }
            } catch (\Exception $exception) {

            }

            $this->status = 1;
            $this->message = '数据返回成功';

            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


    //app退款
    public function refund(Request $request)
    {
        try {
            // return json_encode([
            //     'status' => '2',
            //     'message' => '退款权限已关闭'
            // ]);
            $merchant = $this->parseToken();
            $merchant_id = $merchant->merchant_id;
            $out_trade_no = $request->get('out_trade_no', '');
            $refund_amount = $request->get('refund_amount', '');

            //收银员
            if ($merchant->merchant_type == 2) {
                return json_encode([
                    'status' => '2',
                    'message' => '收银员没有退款权限'
                ]);
            }

            $data = [
                'merchant_id' => $merchant_id,
                'out_trade_no' => $out_trade_no,
                'refund_amount' => $refund_amount
            ];
            $return = $this->refund_public($data);

            return $return;
        } catch (\Exception $ex) {
            return json_encode([
                'status' => '-1',
                'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine()
            ]);
        }
    }


    //app退款-公共
    public function refund_public($data)
    {
        try {
            $merchant_id = $data['merchant_id'];
            $out_trade_no = $data['out_trade_no'];
            //退款加锁
            if(!empty($data['out_trade_no'])){
                $isRun=LockUtil::lock("out_trade_no:".$data['out_trade_no'],"REFUND");
                Log::channel("refund")->info("refund_public退款开始：".$data['out_trade_no'],[
                    "isRun"=>$isRun
                ]);
                if(empty($isRun)){
                    return json_encode([
                        'status' => '2',
                        'message' => '正在退款中请稍等'
                    ]);
                }
                //设置过期时间
//                LockUtil::expire("out_trade_no_" . $data["out_trade_no"],"REFUND");
            }
            $refund_amount = isset($data['refund_amount']) ?  floatval($data['refund_amount']): 0.00;
            $refund_no = isset($data['refund_no']) ? $data['refund_no'] : $out_trade_no . '123';
            if ($refund_no == "") {
                $refund_no = time();
            }
            $a = array('a', '_', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
            $b = str_ireplace($a, "", $out_trade_no);
            $day = substr($b, 0, 8);
            $table = 'orders_' . $day;

            if (!Schema::hasTable($table)) {
                $day = date('Ymd', time());
                $table = 'orders_' . $day;
            }

            if (Schema::hasTable($table)) {
                $order = DB::table($table)->where('trade_no', $out_trade_no);
            } else {
                if(empty($out_trade_no)){
                    $order = Order::where('trade_no', $data['refund_no']);
                }else{
                    $order = Order::where('trade_no', $out_trade_no);
                }
            }
            $order = $order->select(
                'id',
                'total_amount',
                'refund_amount',
                'device_id',
                'out_trade_no',
                'qwx_no',
                'trade_no',
                'store_id',
                'ways_type',
                'config_id',
                'other_no',
                'trade_no',
                'ways_source',
                'created_at',
                'pay_time',
                'rate',
                'buyer_pay_amount',
                'receipt_amount',
                'pay_amount',
                "config_id",
                "company"
            )
                ->first();
            if (!$order) {
                if (Schema::hasTable($table)) {
                    $order = DB::table($table)->where('out_trade_no', $out_trade_no);
                } else {
                    $order = Order::where('out_trade_no', $out_trade_no);
                }
                $order = $order->select(
                    'id',
                    'total_amount',
                    'refund_amount',
                    'device_id',
                    'out_trade_no',
                    'qwx_no',
                    'trade_no',
                    'store_id',
                    'ways_type',
                    'config_id',
                    'other_no',
                    'trade_no',
                    'ways_source',
                    'created_at',
                    'pay_time',
                    'rate',
                    'buyer_pay_amount',
                    'receipt_amount',
                    'pay_amount',
                    'auth_code',
                    "config_id",
                    "company"
                )
                    ->first();
            }
            if (!$order) {
                if (Schema::hasTable($table)) {
                    $order = DB::table($table)->where('other_no', $out_trade_no);
                } else {
                    $order = Order::where('other_no', $out_trade_no);
                }
                $order = $order->select(
                    'id',
                    'total_amount',
                    'refund_amount',
                    'device_id',
                    'out_trade_no',
                    'qwx_no',
                    'trade_no',
                    'store_id',
                    'ways_type',
                    'config_id',
                    'other_no',
                    'trade_no',
                    'ways_source',
                    'created_at',
                    'pay_time',
                    'rate',
                    'buyer_pay_amount',
                    'receipt_amount',
                    'pay_amount',
                    'auth_code',
                    "config_id",
                    "company"
                )
                    ->first();
            }
            
            if (!$order) {
                if (Schema::hasTable($table)) {
                    $order = DB::table($table)->where('qwx_no', $out_trade_no);
                } else {
                    $order = Order::where('qwx_no', $out_trade_no);
                }
                $order = $order->select(
                    'id',
                    'total_amount',
                    'refund_amount',
                    'device_id',
                    'out_trade_no',
                    'qwx_no',
                    'trade_no',
                    'store_id',
                    'ways_type',
                    'config_id',
                    'other_no',
                    'trade_no',
                    'ways_source',
                    'created_at',
                    'pay_time',
                    'rate',
                    'buyer_pay_amount',
                    'receipt_amount',
                    'pay_amount',
                    'auth_code',
                    "config_id",
                    "company"
                )
                    ->first();
            }
            
            if (isset($data['sxf_uuid']) && !empty($data['sxf_uuid'])) {
                $out_trade_no = \App\Models\OrderExtend::where(['user_bar_code' => $data['sxf_uuid']])->value('out_trade_no');
                if (Schema::hasTable($table)) {
                    $order = DB::table($table)->where('out_trade_no', $out_trade_no);
                } else {
                    $order = Order::where('out_trade_no', $out_trade_no);
                }
                $order = $order->select(
                    'id',
                    'total_amount',
                    'refund_amount',
                    'device_id',
                    'out_trade_no',
                    'qwx_no',
                    'trade_no',
                    'store_id',
                    'ways_type',
                    'config_id',
                    'other_no',
                    'trade_no',
                    'ways_source',
                    'created_at',
                    'pay_time',
                    'rate',
                    'buyer_pay_amount',
                    'receipt_amount',
                    'pay_amount',
                    'auth_code',
                    "config_id",
                    "company"
                )
                    ->first();
            }
            
            if (!$order) {
                return json_encode([
                    'status' => 2,
                    'message' => '订单号不存在'
                ]);
            }
            
            if ($order->refund_amount >0) {
                return json_encode([
                    'status' => '2',
                    'message' => '该订单不能重复退款'
                ]);
            }

            //判断退款是否达到
            if ($order->refund_amount >= $order->total_amount) {
                return json_encode([
                    'status' => '2',
                    'message' => '此订单已全部退款'
                ]);
            }

            //判断退款金额是否超交易金额
            $already_refund_amount = ($order->refund_amount * 100); //已退款金额,分
            if ( ( ($refund_amount * 100) + $already_refund_amount) > ($order->total_amount * 100) ) {
                return json_encode([
                    'status' => '2',
                    'message' => '退款金额大于总金额'
                ]);
            }


            //充值订单不支持退款
            if ($order->device_id == "member_cz") {
                return json_encode([
                    'status' => '2',
                    'message' => '充值订单不支持退款'
                ]);
            }

            if (Order::where('out_trade_no', $order->out_trade_no)->value('pay_status') != 1) {
                return json_encode([
                    'status' => '2',
                    'message' => '订单状态异常'
                ]);
            }

            $out_trade_no = $order->out_trade_no;
            $OutRefundNo = $refund_no;
            $refund_amount = isset($refund_amount) && $refund_amount ? $refund_amount : ($order->buyer_pay_amount ? $order->buyer_pay_amount: ($order->receipt_amount ?$order->receipt_amount:$order->pay_amount));

            $store_id = $order->store_id;
            if($store_id=='20222179856741777'|| $store_id=='2022021711112835758' || $store_id=='2022021711122881280'|| $store_id=='2022021711131741222'){
                return json_encode([
                    'status' => 2,
                    'message' => '退款权限已关闭'
                ]);
            }
            $ways_type = $order->ways_type;
            
            $store = Store::where('store_id', $store_id)
                ->select('config_id', 'merchant_id', 'pid', 'is_before_refund')
                ->first();
            $config_id = $store->config_id;
            $store_id = $order->store_id;
            $store_pid = $store->pid;
            $other_no = $order->other_no;
            $out_trade_no = $order->out_trade_no;
            $total_amount = $order->total_amount; //交易总额
            $trade_no = $order->trade_no;
            $rate = $order->rate; //订单但是费率
            $new_refund_amount = $order->refund_amount + $refund_amount; //总退款
            $new_fee_amount = ($total_amount - $new_refund_amount) * ($rate/100); //退款后的手续费
            
            // file_put_contents('1019.log', var_export($order, true) . "\r\n", FILE_APPEND);
            // $order_time = json_decode($order, true);

            if( $order->created_at < date('Y-m-d 00:00:00', time()) && $store->is_before_refund == 2){
                
                $jiujiangMch = ''; 
                if (36999 < $ways_type && $ways_type < 37999) {
                  $config = new JiuJiangConfigController();
                    $jiujiang_merchant = $config->jiujiang_merchant($store_id, $store_pid);
                    if ($jiujiang_merchant) {
                        if ($jiujiang_merchant->mchtNo == '45436105462C030') {
                            $jiujiangMch = '45436105462C030';
                        }
                    }
                }
                
                if ($store_id == '20231191759057240' || $jiujiangMch 
                // || $store_id == '2024082118003398589' 
                
                ) {
                    
                } else {
                     $re_data = [
                        'status' => 2,
                        'message' => '退款失败, 超过退款时效',
                    ];
                    return json_encode($re_data);
                }
                
                
                // if($store_id != '20231191759057240'){
                //     $re_data = [
                //         'status' => 2,
                //         'message' => '退款失败, 超过退款时效',
                //     ];
                //     return json_encode($re_data);
                // }
            }
            // 九江银行 退款
            if (36999 < $ways_type && $ways_type < 37999) {
                $config = new JiuJiangConfigController();
                $jiujiang_merchant = $config->jiujiang_merchant($store_id, $store_pid);
                if (!$jiujiang_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '九江银行商户号不存在'
                    ]);
                }
                
                $txnAmt = substr( strval($refund_amount * 100 + 1000000000000), 1, 15 ); // 交易金额（分，12 位，不足左补 0）
                
                $refund_data = [
                    'requestBranchCode' => '0220200000032',
                    'mchtNo' => $jiujiang_merchant->mchtNo, // 平台分配的商户号
                    'termNo' => $jiujiang_merchant->termNo, // 平台分配的终端号
                    'secMchtNo' => $jiujiang_merchant->mchtNo, 
                    
                    'outOrderNo' => date('YmdHis', time()).time(), 
                    'orderName' => '正常退款', // 交易金额（分，12 位，不足左补 0）
                    'orderTime' => date('His', time()), 
                    'orderDate' => date('md', time()),
                    'txnAmt' => $txnAmt,
                    'oriTxnDate'=>date('Ymd', time()),
                    'codeNo'=>'A4014'.$order->trade_no,
                ];
                
                $obj = new \App\Api\Controllers\JiuJiang\PayController();
                $return = $obj->refund($refund_data,$jiujiang_merchant->private_key); //-1系统错误；0-其他；1-处理成功；2-验签失败
                //成功
                if ($return['status'] == '1') {
                    $insert_data = [
                        'status' => '6',
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    //$refund_amount = ($refund_amount / 100); //金额，单位分

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $refund_no, //联动流水号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $out_trade_no,
                        'trade_no' => $trade_no
                    ]);
                    //退款成功后的操作
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $refund_no, //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['outOrderNo'], //联动流水号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    return json_encode([
                        'status' => '1',
                        'message' => '退款成功',
                        'data' => $data,
                    ]);
                }
                else {
                    //其他情况
                    $message = '退款失败';
                    return json_encode([
                        'status' => '2',
                        'message' => $message
                    ]);
                }
            }
            //国通支付-退款 - 已增加v2
            if(in_array($ways_type,[62001,62002,62004,62011,62012,62014])){
                $params = [
                    'config_id' => $config_id,
                    'store_id' => $store_id,
                    'store_pid' => $store_pid,
                    'out_trade_no' => $out_trade_no,
                    'refund_no' => $refund_no,
                    'refund_amount' => $refund_amount,
                    'new_refund_amount' => $new_refund_amount,
                    'new_fee_amount' => $new_fee_amount,
                    'ways_type' => $ways_type,
                    'merchant_id' => $merchant_id,
                    'trade_no' => $trade_no
                ];
                return BizService::gtxy_refund($order,$params,$this);
            }
            
            //支付宝官方
            if (999 < $ways_type && $ways_type < 1999) {
                //配置
                $isvconfig = new AlipayIsvConfigController();
                $config_type = '01';
                $config = $isvconfig->AlipayIsvConfig($config_id, $config_type);

                //获取token
                $storeInfo = $isvconfig->alipay_auth_info($store_id, $store_pid);

                $aop = new AopClient();
                $aop->apiVersion = "2.0";
                $aop->appId = $config->app_id;
                $aop->rsaPrivateKey = $config->rsa_private_key;
                $aop->alipayrsaPublicKey = $config->alipay_rsa_public_key;
                $aop->signType = "RSA2"; //升级算法
                $aop->gatewayUrl = $config->alipay_gateway;
                $aop->format = "json";
                $aop->charset = "GBK";
                $aop->version = "2.0";
                $aop->method = "alipay.trade.refund";

                $requests = new AlipayTradeRefundRequest();
                $data_req_ali = "{" .
                    "\"out_trade_no\":\"" . $out_trade_no . "\"," .
                    "\"refund_amount\":\"" . $refund_amount . "\"," .
                    "\"out_request_no\":\"" . $OutRefundNo . "\"," .
                    "\"refund_reason\":\"正常退款\"" .
                    "}";
                $requests->setBizContent($data_req_ali);
                $result = $aop->execute($requests, null, $storeInfo->app_auth_token);
                $responseNode = str_replace(".", "_", $requests->getApiMethodName()) . "_response";
                $resultCode = $result->$responseNode->code;

                //退款成功
                if (!empty($resultCode) && $resultCode == 10000) {

                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount,//退款金额
                        'refund_no' => $OutRefundNo,//退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    //退款成功后的操作
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $OutRefundNo, //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $OutRefundNo,//退款单号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    return json_encode([
                        'status' => '1',
                        'message' => '退款成功',
                        'data' => $data
                    ]);
                } else {
                    //退款失败
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                    ];

                    return json_encode([
                        'status' => 2,
                        'message' => $result->$responseNode->sub_msg,
                        'data' => $data,
                    ]);
                }
            }

            //直付通
            if (16000 < $ways_type && $ways_type < 16999) {
                $config_type = '03';
                //配置
                $isvconfig = new AlipayIsvConfigController();
                $config = $isvconfig->AlipayIsvConfig($config_id, $config_type);
                if (!$config) {
                    return json_encode([
                        'status' => '2',
                        'message' => '直付通配置不存在'
                    ]);
                }

                $aop = new AopClient();
                $aop->apiVersion = "2.0";
                $aop->appId = $config->app_id;
                $aop->rsaPrivateKey = $config->rsa_private_key;
                $aop->alipayrsaPublicKey = $config->alipay_rsa_public_key;
                $aop->signType = "RSA2"; //升级算法
                $aop->gatewayUrl = $config->alipay_gateway;
                $aop->format = "json";
                $aop->charset = "GBK";
                $aop->version = "2.0";
                $aop->method = "alipay.trade.refund";

                $requests = new AlipayTradeRefundRequest();
                $data_re = array(
                    'out_trade_no' => $out_trade_no,
                    'refund_amount' => $refund_amount,
                    'out_request_no' => $OutRefundNo,
                    'refund_reason' => '正常退款',
                );

                $a = array('a', '_', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
                $b = str_ireplace($a, "", $out_trade_no);
                $day = substr($b, 0, 8);
                $table = 'settle_orders_' . $day;
                if (Schema::hasTable($table)) {
                    $settle_orders = DB::table($table)->where('out_trade_no', $out_trade_no)
                        ->where('store_id', $store_id)
                        ->first();
                    if ($settle_orders && $settle_orders->order_settle_amount > 0) {
                        $data_re['refund_royalty_parameters'] = array(
                            0 => array(
                                'trans_out' => $settle_orders->trans_out,
                                'amount' => $settle_orders->order_settle_amount,
                                'desc' => '退款分账'
                            )
                        );
                    }
                }

                $data_re = json_encode($data_re);
                $requests->setBizContent($data_re);
                $result = $aop->execute($requests, null, '');
                $responseNode = str_replace(".", "_", $requests->getApiMethodName()) . "_response";
                $resultCode = $result->$responseNode->code;

                //退款成功
                if (!empty($resultCode) && $resultCode == 10000) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $OutRefundNo, //退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    //退款成功后的操作
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $OutRefundNo, //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $OutRefundNo, //退款单号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    return json_encode([
                        'status' => '1',
                        'message' => '退款成功',
                        'data' => $data
                    ]);
                } else {
                    //退款失败
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                    ];

                    return json_encode([
                        'status' => '2',
                        'message' => $result->$responseNode->sub_msg,
                        'data' => $data
                    ]);
                }
            }

            //微信官方扫码退款
            if (1999 < $ways_type && $ways_type < 2999) {
                $config = new WeixinConfigController();
                $options = $config->weixin_config($config_id);
                $weixin_store = $config->weixin_merchant($store_id, $store_pid);
                $wx_sub_merchant_id = $weixin_store->wx_sub_merchant_id;

                $config = [
                    'app_id' => $options['app_id'],
                    'mch_id' => $options['payment']['merchant_id'],
                    'key' => $options['payment']['key'],
                    'cert_path' => $options['payment']['cert_path'], // XXX: 绝对路径！！！！
                    'key_path' => $options['payment']['key_path'],     // XXX: 绝对路径！！！！
                    'sub_mch_id' => $wx_sub_merchant_id,
                    // 'device_info'     => '013467007045764',
                    // 'sub_app_id'      => '',
                    // ...
                ];

                $payment = Factory::payment($config);
                // 参数分别为：商户订单号、商户退款单号、订单金额、退款金额、其他参数
                $refund = $payment->refund->byOutTradeNumber($order->out_trade_no, $OutRefundNo, $order->total_amount * 100, $refund_amount  * 100);

                if ($refund['return_code'] == "SUCCESS") {
                    //退款成功
                    if ($refund['result_code'] == "SUCCESS") {
                        $insert_data = [
                            'status' => 6,
                            'pay_status' => 6,
                            'pay_status_desc' => '已退款',
                            'refund_amount' => $new_refund_amount,
                            'fee_amount' => $new_fee_amount
                        ];
                        $this->update_day_order($insert_data, $out_trade_no);

                        RefundOrder::create([
                            'ways_source' => $order->ways_source,
                            'type' => $ways_type,
                            'refund_amount' => $refund_amount, //退款金额
                            'refund_no' => $OutRefundNo, //退款单号
                            'store_id' => $store_id,
                            'merchant_id' => $merchant_id,
                            'out_trade_no' => $order->out_trade_no,
                            'trade_no' => $order->trade_no
                        ]);
                        //退款成功后的操作
                        RefundSuccessAction::action([
                            'out_trade_no'=>$out_trade_no,
                            'refund_no' => $OutRefundNo, //联动流水号
                        ]);
                        $data = [
                            'refund_amount' => $refund_amount,
                            'out_trade_no' => $out_trade_no,
                            'refund_no' => $OutRefundNo, //退款单号
                            'other_no' => $other_no
                        ];

                        //返佣去掉
                        UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                            'settlement' => '03',
                            'settlement_desc' => '退款订单',
                        ]);
                        MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                            'settlement' => '03',
                            'settlement_desc' => '退款订单',
                        ]);

                        return json_encode([
                            'status' => 1,
                            'message' => '退款成功',
                            'data' => $data,
                        ]);
                    } else {
                        $data = [
                            'refund_amount' => $refund_amount,
                            'out_trade_no' => $out_trade_no,
                        ];

                        return json_encode([
                            'status' => 2,
                            'message' => $refund['err_code_des'],
                            'data' => $data,
                        ]);
                    }
                } else {
                    //退款失败
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                    ];

                    return json_encode([
                        'status' => '2',
                        'message' => $refund['return_msg'],
                        'data' => $data
                    ]);
                }
            }

            //微信官方a 扫码退款
            if (3999 < $ways_type && $ways_type < 4999) {
                $config = new WeixinConfigController();
                $options = $config->weixina_config($config_id);
                $weixin_store = $config->weixina_merchant($store_id, $store_pid);
                $wx_sub_merchant_id = $weixin_store->wx_sub_merchant_id;

                $config = [
                    'app_id' => $options['app_id'],
                    'mch_id' => $options['payment']['merchant_id'],
                    'key' => $options['payment']['key'],
                    'cert_path' => $options['payment']['cert_path'], // XXX: 绝对路径！！！！
                    'key_path' => $options['payment']['key_path'],     // XXX: 绝对路径！！！！
                    'sub_mch_id' => $wx_sub_merchant_id,
                    // 'device_info'     => '013467007045764',
                    // 'sub_app_id'      => '',
                    // ...
                ];
                $payment = Factory::payment($config);

                // 参数分别为：商户订单号、商户退款单号、订单金额、退款金额、其他参数
                $refund = $payment->refund->byOutTradeNumber($order->out_trade_no, $OutRefundNo, $order->total_amount * 100, $refund_amount  * 100);
                if ($refund['return_code'] == "SUCCESS") {
                    //退款成功
                    if ($refund['result_code'] == "SUCCESS") {
                        $insert_data = [
                            'status' => 6,
                            'pay_status' => 6,
                            'pay_status_desc' => '已退款',
                            'refund_amount' => $new_refund_amount,
                            'fee_amount' => $new_fee_amount
                        ];
                        $this->update_day_order($insert_data, $out_trade_no);

                        RefundOrder::create([
                            'ways_source' => $order->ways_source,
                            'type' => $ways_type,
                            'refund_amount' => $refund_amount, //退款金额
                            'refund_no' => $OutRefundNo, //退款单号
                            'store_id' => $store_id,
                            'merchant_id' => $merchant_id,
                            'out_trade_no' => $order->out_trade_no,
                            'trade_no' => $order->trade_no
                        ]);
                        //退款成功后的操作
                        RefundSuccessAction::action([
                            'out_trade_no'=>$out_trade_no,
                            'refund_no' => $OutRefundNo, //联动流水号
                        ]);
                        $data = [
                            'refund_amount' => $refund_amount,
                            'out_trade_no' => $out_trade_no,
                            'refund_no' => $OutRefundNo, //退款单号
                            'other_no' => $other_no
                        ];

                        //返佣去掉
                        UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                            'settlement' => '03',
                            'settlement_desc' => '退款订单',
                        ]);
                        MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                            'settlement' => '03',
                            'settlement_desc' => '退款订单',
                        ]);

                        return json_encode([
                            'status' => 1,
                            'message' => '退款成功',
                            'data' => $data,
                        ]);
                    } else {
                        $data = [
                            'refund_amount' => $refund_amount,
                            'out_trade_no' => $out_trade_no,
                        ];

                        return json_encode([
                            'status' => 2,
                            'message' => $refund['err_code_des'],
                            'data' => $data,
                        ]);
                    }
                } else {
                    //退款失败
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                    ];

                    return json_encode([
                        'status' => '2',
                        'message' => $refund['return_msg'],
                        'data' => $data
                    ]);
                }
            }

            //京东收银通道
            if (5999 < $ways_type && $ways_type < 6999) {
                //读取配置
                $config = new JdConfigController();
                $jd_config = $config->jd_config($config_id);
                if (!$jd_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '京东配置不存在请检查配置'
                    ]);
                }

                $jd_merchant = $config->jd_merchant($store_id, $store_pid);
                if (!$jd_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '京东商户号不存在'
                    ]);
                }
                $obj = new \App\Api\Controllers\Jd\PayController();
                $data = [];
                $data['out_trade_no'] = $out_trade_no;
                $data['request_url'] = $obj->refund_url; //请求地址;
                $data['notifyUrl'] = url('/api/jd/refund_url'); //通知地址;
                $data['merchant_no'] = $jd_merchant->merchant_no;
                $data['md_key'] = $jd_merchant->md_key; //
                $data['des_key'] = $jd_merchant->des_key; //
                $data['systemId'] = $jd_config->systemId; //
                $data['outRefundNo'] = $OutRefundNo;
                $data['amount'] = $order->total_amount;

                $return = $obj->refund($data);

                //退款请求成功
                if ($return["status"] == 1) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $OutRefundNo, //退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    //退款成功后的操作
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' =>$OutRefundNo, //平台退款单号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $OutRefundNo, //退款单号
                        'other_no' => $other_no

                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    return json_encode([
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }

            //快钱支付通道
            if (2999 < $ways_type && $ways_type < 3999) {
                //读取配置
                $config = new MyBankConfigController();
                $MyBankConfig = $config->MyBankConfig($config_id);
                if (!$MyBankConfig) {
                    return json_encode([
                        'status' => 2,
                        'message' => '快钱配置不存在请检查配置'
                    ]);
                }

                $mybank_merchant = $config->mybank_merchant($store_id, $store_pid);
                if (!$mybank_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '快钱商户号不存在'
                    ]);
                }

                $obj = new TradePayController();
                $MerchantId = $mybank_merchant->MerchantId;
                $return = $obj->mybankrefund($MerchantId, $out_trade_no, $OutRefundNo, $refund_amount, $config_id);

                //退款请求成功
                if ($return["status"] == 1) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount,//退款金额
                        'refund_no' => $OutRefundNo,//退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    //退款成功后的操作
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $OutRefundNo, //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $OutRefundNo,//退款单号
                        'other_no' => $other_no

                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    return json_encode([
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }

            //新大陆
            if (7999 < $ways_type && $ways_type < 8999) {
                if ($refund_amount && $refund_amount != $order->total_amount) {
                    return json_encode([
                        'status' => 2,
                        'message' => '只支持退全额'
                    ]);
                }

                //读取配置
                $config = new NewLandConfigController();
                $new_land_config = $config->new_land_config($config_id);
                if (!$new_land_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '新大陆配置不存在请检查配置'
                    ]);
                }

                $new_land_merchant = $config->new_land_merchant($store_id, $store_pid);
                if (!$new_land_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户新大陆通道未开通'
                    ]);
                }
                $request_data = [
                    'out_trade_no' => $out_trade_no,
                    'trade_no' => $order->trade_no,
                    'key' => $new_land_merchant->nl_key,
                    'org_no' => $new_land_config->org_no,
                    'merc_id' => $new_land_merchant->nl_mercId,
                    'trm_no' => $new_land_merchant->trmNo,
                    'op_sys' => '3',
                    'opr_id' => $store->merchant_id,
                    'trm_typ' => 'T',
                ];
                $obj = new PayController();
                $return = $obj->refund($request_data);

                //退款请求成功
                if ($return["status"] == 1) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount,//退款金额
                        'refund_no' => $OutRefundNo,//退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    //退款成功后的操作
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' =>$OutRefundNo, //平台退款单号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $OutRefundNo,//退款单号
                        'other_no' => $other_no

                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    return json_encode([
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }

            //和融通
            if (8999 < $ways_type && $ways_type < 9999) {
                if ($refund_amount && $refund_amount != $order->total_amount) {
                    return json_encode([
                        'status' => 2,
                        'message' => '只支持退全额'
                    ]);
                }
                //读取配置
                $config = new HConfigController();
                $h_config = $config->h_config($config_id);
                if (!$h_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '和融通配置不存在请检查配置'
                    ]);
                }

                $h_merchant = $config->h_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '和融通商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Huiyuanbao\PayController();
                $data = [];
                $data['trade_no'] = $order->trade_no;
                $data['request_url'] = $obj->refund_url; //请求地址;
                $data['notifyUrl'] = url('/api/jd/refund_url'); //通知地址;
                $data['mid'] = $h_merchant->h_mid;
                $data['md_key'] = $h_config->md_key; //
                $data['orgNo'] = $h_merchant->orgNo; //
                $data['outRefundNo'] = $OutRefundNo;
                $data['amount'] = $order->total_amount;
                $return = $obj->refund($data);

                //退款请求成功
                if ($return["status"] == 1) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount,//退款金额
                        'refund_no' => $return['data']['refundOrderNo'],//退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    //退款成功后的操作
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' =>$return['data']['refundOrderNo'],//退款单号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['refundOrderNo'],//退款单号
                        'other_no' => $other_no

                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    return json_encode([
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }

            //lft收银通道
            if (9999 < $ways_type && $ways_type < 10999) {
                //读取配置
                $config = new LtfConfigController();
                $ltf_merchant = $config->ltf_merchant($store_id, $store_pid);
                if (!$ltf_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Ltf\PayController();
                $data = [];
                $data['out_trade_no'] = $out_trade_no;
                $data['request_url'] = $obj->refund_url; //请求地址;
                $data['notifyUrl'] = url('/api/jd/refund_url'); //通知地址;
                $data['merchant_no'] = $ltf_merchant->merchantCode;
                $data['appId'] = $ltf_merchant->appId; //
                $data['key'] = $ltf_merchant->md_key; //
                $data['outRefundNo'] = $OutRefundNo;
                $data['amount'] = $refund_amount;
                $return = $obj->refund($data);

                //退款请求成功
                if ($return["status"] == 1) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount,//退款金额
                        'refund_no' => $OutRefundNo,//退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    //退款成功后的操作
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' =>$OutRefundNo, //平台退款单号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $OutRefundNo,//退款单号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    return json_encode([
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }

            //随行付收银通道
            if (12999 < $ways_type && $ways_type < 13999) {
                //读取配置
                $config = new VbillConfigController();
                $vbill_config = $config->vbill_config($config_id);
                if (!$vbill_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '随行付配置不存在请检查配置'
                    ]);
                }

                $vbill_merchant = $config->vbill_merchant($store_id, $store_pid);
                if (!$vbill_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '随行付商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Vbill\PayController();
                $data['notify_url'] = url('/api/vbill/refund_notify_url'); //退款回调地址
                $data['request_url'] = $obj->refund_url; //请求地址;
                $data['mno'] = $vbill_merchant->mno;
                $data['privateKey'] = $vbill_config->privateKey; //
                $data['sxfpublic'] = $vbill_config->sxfpublic; //
                $data['orgId'] = $vbill_config->orgId; //
                $data['out_trade_no'] = $order->out_trade_no;
                $data['refund_amount'] = $refund_amount;
                $return = $obj->refund($data);
                file_put_contents('b09754.log',var_export($return,true)."\r\n",FILE_APPEND);
                //退款请求成功
                if ($return["status"] == 1) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $OutRefundNo, //退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    //退款成功后的操作
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' =>$OutRefundNo, //平台退款单号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $OutRefundNo, //退款单号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    return json_encode([
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ]);
                } elseif($return["status"] == 2){

                    RefundOrderDoing::create([
                        "company"=>$order->company,
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $OutRefundNo, //退款单号
                        'other_refund_no' => $OutRefundNo, //退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    
                    return json_encode([
                        'status' => 1,
                        'message' => '退款中',
                        'data' => $data,
                    ]);
                }else {
                    //其他情况
                    $message = $return['data']['respData']['tradeMsg'];
                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }

            //随行付a收银通道
            if (18999 < $ways_type && $ways_type < 19999) {
                //读取配置
                $config = new VbillConfigController();
                $vbill_config = $config->vbilla_config($config_id);
                if (!$vbill_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '随行付A配置不存在请检查配置'
                    ]);
                }

                $vbill_merchant = $config->vbilla_merchant($store_id, $store_pid);
                if (!$vbill_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '随行付A商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Vbill\PayController();
                $data['notify_url'] = url('/api/vbill/refund_notify_url'); //退款回调地址
                $data['request_url'] = $obj->refund_url; //请求地址;
                $data['mno'] = $vbill_merchant->mno;
                $data['privateKey'] = $vbill_config->privateKey; //
                $data['sxfpublic'] = $vbill_config->sxfpublic; //
                $data['orgId'] = $vbill_config->orgId; //
                $data['out_trade_no'] = $order->out_trade_no;
                $data['refund_amount'] = $refund_amount;

                $return = $obj->refund($data);

                //退款请求成功
                if ($return["status"] == 1) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $OutRefundNo, //退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    //退款成功后的操作
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' =>$OutRefundNo, //平台退款单号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $OutRefundNo,//退款单号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    return json_encode([
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }

            //哆啦宝
            if (14999 < $ways_type && $ways_type < 15999) {
                if ($refund_amount && $refund_amount != $order->total_amount) {
                    return json_encode([
                        'status' => 2,
                        'message' => '只支持退全额'
                    ]);
                }

                $OutRefundNo = "dlbscan" . date('YmdHis') . str_pad(rand(0, 9999), 4, 0);
                $manager = new ManageController();
                $dlb_config = $manager->pay_config($config_id);

                if (!$dlb_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '哆啦宝配置配置不存在请检查配置'
                    ]);
                }
                $dlb_merchant = $manager->dlb_merchant($store_id, $store_pid);
                if (!$dlb_merchant && !empty($dlb_merchant->mch_num) && !empty($dlb_merchant->shop_num)) {
                    return json_encode([
                        'status' => 2,
                        'message' => '哆啦宝配置商户未补充商户编号等信息!'
                    ]);
                }
                $refund_data = [
                    "accessKey" => $dlb_config->access_key,
                    "secretKey" => $dlb_config->secret_key,
                    "agentNum" => $dlb_config->agent_num,
                    "customerNum" => $dlb_merchant->mch_num,
                    "shopNum" => $dlb_merchant->shop_num,
                    "requestNum" => $out_trade_no,
                    "refundRequestNum" => $OutRefundNo,
                ];
                $return = $manager->pay_refund($refund_data);
                if ($return['status'] == 1) {
                    //退款请求成功
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount,//退款金额
                        'refund_no' => $OutRefundNo,//退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    //退款成功后的操作
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' =>$OutRefundNo, //平台退款单号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $OutRefundNo,//退款单号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    return json_encode([
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ]);
                } else {
                    return json_encode($return);
                }
            }

            //传化
            if (11999 < $ways_type && $ways_type < 12999) {
                //读取配置
                $config = new TfConfigController();

                $h_merchant = $config->tf_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '传化商户号不存在'
                    ]);
                }

                $h_config = $config->tf_config($config_id, $h_merchant->qd);
                if (!$h_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '传化配置不存在请检查配置'
                    ]);
                }

                $obj = new \App\Api\Controllers\Tfpay\PayController();
                $data['mch_id'] = $h_config->mch_id; //
                $data['pub_key'] = $h_config->pub_key; //
                $data['pri_key'] = $h_config->pri_key; //
                $data['sub_mch_id'] = $h_merchant->sub_mch_id; //
                $data['out_trade_no'] = $out_trade_no; //
                $data['date'] = date('Y-m-d', time()); //
                $data['refund_trade_no'] = time(); //
                $data['refund_fee'] = $refund_amount; //

                $return = $obj->refund($data);

                //退款请求成功
                if ($return["status"] == 1) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount,//退款金额
                        'refund_no' => $return['data']['refund_trade_no'],//退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    //退款成功后的操作
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $return['data']['refund_trade_no'],//退款单号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['refund_trade_no'],//退款单号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    $re_data = [
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ];

                    return json_encode($re_data);
                } else {
                    //其他情况
                    $message = $return['message'];

                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }

            //工行
            if (20001 == $ways_type || 20002 == $ways_type || 20004 == $ways_type) {
                $config = new LianfuConfigController();
                $h_merchant = $config->lianfu_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '工行商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\lianfu\PayController();
                $data['apikey'] = $h_merchant->apikey; //
                $data['signkey'] = $h_merchant->signkey; //
                $data['appid'] = $h_merchant->appid; //
                $data['out_trade_no'] = $out_trade_no; //
                $data['total_amount'] = $refund_amount; //
                $return = $obj->refund($data);
                //退款请求成功
                if ($return["status"] == 1) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $out_trade_no, //退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $out_trade_no, //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $out_trade_no, //退款单号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    $re_data = [
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ];

                    return json_encode($re_data);
                } else {
                    //其他情况
                    $message = $return['message'];

                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }
            
            //银商
//            if (30020 == $ways_type || 30021 == $ways_type || 30022 == $ways_type) {
            if(in_array($ways_type,[30020,30021,30022,30023,30024,30025])){
                $config = new MerchConfigController();

                $version="v1";
                if(in_array($ways_type,[30023,30024,30025])){
                    $version="v2";
                }

                $h_merchant = $config->merch_merchant($store_id, $store_pid,$version);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '银联商务商户号不存在'
                    ]);
                }

                $return_data = [
                    'merchantCode' => $h_merchant->MerchantId,
                    'terminalCode' => $h_merchant->merchzhongduanid,
                    'merchantOrderId' => $out_trade_no,
                    'refundRequestId' => $out_trade_no,
                    'transactionAmount' => intval($order->total_amount*100)
                ];

                $obj = new WftPayPayController();
                if(in_array($ways_type,[30020,30021,30022,])){
                    $return = $obj->refund1($h_merchant->accessToken,$return_data);
                }else if(in_array($ways_type,[30023,30024,30025])){
                    $return_data["appId"] = $h_merchant->merch_appid;
                    $return_data["appKey"] = $h_merchant->merch_appkey;
                    $return_data["store_id"] = $store_id;
                    $return = $obj->refundV6($return_data);
//                    $return = $obj->voidPaymentV6($return_data);
                }
                //退款请求成功
                if ($return["status"] == 1) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $out_trade_no, //退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $out_trade_no, //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $out_trade_no, //退款单号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    $re_data = [
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ];

                    return json_encode($re_data);
                } else {
                    //其他情况
                    $message = $return['message'];

                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }
            //海科融通 退款
            if (21999 < $ways_type && $ways_type < 22999) {
                //读取配置
                $config = new HkrtConfigController();
                $hkrt_config = $config->hkrt_config($config_id);
                if (!$hkrt_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '海科融通配置不存在请检查配置'
                    ]);
                }

                $hkrt_merchant = $config->hkrt_merchant($store_id, $store_pid);
                if (!$hkrt_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '海科融通商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Hkrt\PayController();
                $hkrt_return_data = [
                    'access_id' => $hkrt_config->access_id,
                    'refund_amount' => $refund_amount, //退款金额,（银联二维码只能全额退款）退款金额，以元为单位
                    'trade_no'  => $order->trade_no, //SaaS平台的交易订单编号,trade_no、out_trade_no、channel_trade_no必传其中一个，都传则以trade_no为准，推荐使用trade_no
                    'out_trade_no' => $order->out_trade_no, //服务商退款订单号
                    'notify_url' => url('/api/hkrt/refund_notify_url'), //退款成功后的通知地址
                    'access_key' => $hkrt_config->access_key
                ];
                Log::info('海科融通-退款');
                Log::info($hkrt_return_data);
                $return = $obj->refund($hkrt_return_data); //0-系统错误 1-成功 2-失败 3-结果未知
                Log::info($return);

                //退款请求成功
                if ($return['status'] == '1') {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $order->refund_amount + $return['data']['refunded_amount'],
                        'fee_amount' => 0,
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $return['data']['refunded_amount'], //退款金额
                        'refund_no' => $return['data']['refund_no'], //退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$order->out_trade_no,
                        'refund_no' => $return['data']['refund_no'], //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $return['data']['refunded_amount'],
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['refund_no'], //退款单号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    return json_encode([
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ]);
                } else {
                    //其他情况
                    $message = $return['data']['message'];
                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }

             //易生支付 退款61002
            if (in_array($ways_type,[21001,21002,21004,61001,61002,61004])) {
                //读取配置
                $version="easypay";
                if(in_array($ways_type,[61001,61002,61004])){
                    $version="easypaya";
                }
                $config = new EasyPayConfigController();
                $easypay_config = $config->easypay_config($config_id);
                if (!$easypay_config) {
                    return json_encode([
                        'status' => '2',
                        'message' => '易生支付配置不存在请检查配置'
                    ]);
                }

                $easypay_merchant = $config->easypay_merchant($store_id, $store_pid,$version);
                if (!$easypay_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '易生支付商户号不存在6'
                    ]);
                }

                //当天的交易只能全额撤销,日切时间为渠道服务器的00:00；非当日才可退款
                $obj = new \App\Api\Controllers\EasyPay\PayController();
                $now_time_start = strtotime(date('Y-m-d 00:00:00', time()));
                $now_time_end = strtotime(date('Y-m-d 23:59:59', time()));
                $order_pay_time = strtotime($order->pay_time);

                $easypay_return_data = [
                    'channel_id' => $easypay_config->channel_id,
                    'mer_id' => $easypay_merchant->termMercode, //终端商戶编号
                    'term_id'  => $easypay_merchant->termTermcode, //终端编号
                    'out_trade_no' => $out_trade_no, //订单号
                    'refund_amount' => $refund_amount, //退货金额,不大于原交易金额与已成功退货金额之差
                    'private_key' => $easypay_config->private_key
                ];
                $return = $obj->refund($easypay_return_data); // -1 系统错误 0-其他 1-成功 2-失败


                //退款请求成功
                if ($return['status'] == '1') {
                    $insert_data = [
                        'status' => '6',
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $return['data']['orgTrace'], //易生退货单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$order->out_trade_no,
                        'refund_no' => $return['data']['orgTrace'], //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['orgTrace'], //易生退款单号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    return json_encode([
                        'status' => '1',
                        'message' => '退款成功',
                        'data' => $data,
                    ]);
                }
                else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => '2',
                        'message' => $message
                    ]);
                }
            }

            //易生支付 退款
            if (false) {
                //读取配置
                $config = new EasyPayConfigController();
                $easypay_config = $config->easypay_config($config_id);
                if (!$easypay_config) {
                    return json_encode([
                        'status' => '2',
                        'message' => '易生支付配置不存在请检查配置'
                    ]);
                }

                $easypay_merchant = $config->easypay_merchant($store_id, $store_pid);
                if (!$easypay_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '易生支付商户号不存在5'
                    ]);
                }

                //当天的交易只能全额撤销,日切时间为渠道服务器的00:00；非当日才可退款
                $obj = new \App\Api\Controllers\EasyPay\PayController();
                $now_time_start = strtotime(date('Y-m-d 00:00:00', time()));
                $now_time_end = strtotime(date('Y-m-d 23:59:59', time()));
                $order_pay_time = strtotime($order->pay_time);

                if (($order_pay_time > $now_time_start) && ($order_pay_time <= $now_time_end)) {
                    if ($refund_amount && $refund_amount != $order->total_amount) {
                        return json_encode([
                            'status' => '2',
                            'message' => '当日交易只支持退全额'
                        ]);
                    }

                    $easypay_revoke_data = [
                        'channel_id' => $easypay_config->channel_id, //渠道编号
                        'mno' => $easypay_merchant->termMercode, //终端商戶编号
                        'device_id' => $easypay_merchant->termTermcode, //终端编号
                        'out_trade_no' => $out_trade_no, //订单号
                        'trade_no' => $order->trade_no //系统订单号
                    ];
                    Log::info('易生支付-当日交易撤销');
                    Log::info($easypay_revoke_data);
                    $return = $obj->order_close($easypay_revoke_data); //-1 系统错误 0-其他 1-成功
                    Log::info($return);

                    //成功
                    if ($return['status'] == '1') {
                        $insert_data = [
                            'status' => '6',
                            'pay_status' => 6,
                            'pay_status_desc' => '已退款',
                            'refund_amount' => $new_refund_amount,
                            'fee_amount' => $new_fee_amount
                        ];
                        $this->update_day_order($insert_data, $out_trade_no);

                        $refund_amount = isset($return['data']['clearamt'])? ($return['data']['clearamt'] / 100) : $refund_amount; //结算金额，单位分

                        RefundOrder::create([
                            'ways_source' => $order->ways_source,
                            'type' => $ways_type,
                            'refund_amount' => $refund_amount, //退款金额
                            'refund_no' => $return['data']['oriwtorderid'], //撤销的商户订单号
                            'store_id' => $store_id,
                            'merchant_id' => $merchant_id,
                            'out_trade_no' => $order->out_trade_no,
                            'trade_no' => $order->trade_no
                        ]);
                        RefundSuccessAction::action([
                            'out_trade_no'=>$order->out_trade_no,
                            'refund_no' => $return['data']['oriwtorderid'], //联动流水号
                        ]);
                        $data = [
                            'refund_amount' => $refund_amount,
                            'out_trade_no' => $out_trade_no,
                            'refund_no' => $return['data']['oriwtorderid'], //撤销的商户订单号
                            'other_no' => $other_no
                        ];

                        //返佣去掉
                        UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                            'settlement' => '03',
                            'settlement_desc' => '退款订单',
                        ]);
                        MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                            'settlement' => '03',
                            'settlement_desc' => '退款订单',
                        ]);

                        return json_encode([
                            'status' => '1',
                            'message' => '退款成功',
                            'data' => $data,
                        ]);
                    }
                    else {
                        //其他情况
                        $message = $return['message'];
                        return json_encode([
                            'status' => '2',
                            'message' => $message
                        ]);
                    }
                } else {
                    $easypay_return_data = [
                        'channel_id' => $easypay_config->channel_id, //渠道编号
                        'mer_id' => $easypay_merchant->termMercode, //终端商戶编号
                        'term_id'  => $easypay_merchant->termTermcode, //终端编号
                        'out_trade_no' => $out_trade_no, //订单号
                        'trade_no' => $order->trade_no, //系统订单号
                        'refund_amount' => $refund_amount, //退货金额,不大于原交易金额与已成功退货金额之差
                    ];
                    Log::info('易生支付-退款');
                    Log::info($easypay_return_data);
                    $return = $obj->refund($easypay_return_data); // -1 系统错误 0-其他 1-成功 2-失败
                    Log::info($return);

                    //退款请求成功
                    if ($return['status'] == '1') {
                        $insert_data = [
                            'status' => '6',
                            'pay_status' => 6,
                            'pay_status_desc' => '已退款',
                            'refund_amount' => $new_refund_amount,
                            'fee_amount' => $new_fee_amount
                        ];
                        $this->update_day_order($insert_data, $out_trade_no);

                        RefundOrder::create([
                            'ways_source' => $order->ways_source,
                            'type' => $ways_type,
                            'refund_amount' => $refund_amount, //退款金额
                            'refund_no' => $return['data']['oriwtorderid'], //易生退货单号
//                        'refund_no' => $return['data']['refundwtorderid'], //需要查询的交易订单号（请求报文原值返回）
                            'store_id' => $store_id,
                            'merchant_id' => $merchant_id,
                            'out_trade_no' => $order->out_trade_no,
                            'trade_no' => $order->trade_no
                        ]);
                        RefundSuccessAction::action([
                            'out_trade_no'=>$order->out_trade_no,
                            'refund_no' => $return['data']['oriwtorderid'], //联动流水号
                        ]);
                        $data = [
                            'refund_amount' => $refund_amount,
                            'out_trade_no' => $out_trade_no,
                            'refund_no' => $return['data']['oriwtorderid'], //易生退款单号
                            'other_no' => $other_no
                        ];

                        //返佣去掉
                        UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                            'settlement' => '03',
                            'settlement_desc' => '退款订单',
                        ]);
                        MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                            'settlement' => '03',
                            'settlement_desc' => '退款订单',
                        ]);

                        return json_encode([
                            'status' => '1',
                            'message' => '退款成功',
                            'data' => $data,
                        ]);
                    }
                    else {
                        //其他情况
                        $message = $return['message'];
                        return json_encode([
                            'status' => '2',
                            'message' => $message
                        ]);
                    }
                }
            }

            //邮政 退款
            if (26000 < $ways_type && $ways_type < 26999) {
                $config = new LianfuyoupayConfigController();
                $h_merchant = $config->lianfu_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '邮政商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\lianfuyouzheng\PayController();
                $data['apikey'] = $h_merchant->apikey; //
                $data['signkey'] = $h_merchant->signkey; //
                $data['out_trade_no'] = $out_trade_no; //
                $data['refund_no'] = time(); //
                $data['pay_amount'] = $refund_amount; //
                $data['pos_sn'] = $h_merchant->pos_sn; //
                $data['store_id'] = $store_id; //
                $return = $obj->refund($data);

                //退款请求成功
                if ($return["status"] == 1) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $out_trade_no, //退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $out_trade_no, //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $out_trade_no, //退款单号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    $re_data = [
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ];

                    return json_encode($re_data);
                } else {
                    //其他情况
                    $message = $return['message'];

                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }

            //苏州银行
            if (17000 < $ways_type && $ways_type < 17999) {
                $config = new SuzhouConfigController();
                $h_merchant = $config->sz_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户号不存在'
                    ]);
                }

                $h_config = $config->sz_config($config_id);
                if (!$h_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '配置不存在请检查配置'
                    ]);
                }

                $obj = new \App\Api\Controllers\Suzhou\PayController();
                $data['certId'] = $h_config->certId; //
                $data['pri_key'] = $h_config->rsa_pr; //
                $data['merchantId'] = $h_merchant->MerchantId; //
                $data['orderTime'] = date('YmdHis', time()); //
                $data['oldOrderTime'] = date('YmdHis', strtotime($order->created_at)); //
                $data['refundAmount'] = number_format($refund_amount * 100, 0, '.', '');

                $return = $obj->refund($data);
                //退款请求成功
                if ($return["status"] == 1) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount,//退款金额
                        'refund_no' => $return['data']['tradeNo'],//退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $return['data']['tradeNo'], //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['tradeNo'],//退款单号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    $re_data = [
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ];

                    return json_encode($re_data);
                } else {
                    //其他情况
                    $message = $return['message'];

                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }

            //葫芦天下
            if (22999 < $ways_type && $ways_type < 23999) {
                $manager = new \App\Api\Controllers\Hltx\ManageController();

                $hltx_merchant = $manager->pay_merchant($store_id, $store_pid);
                $qd = $hltx_merchant->qd;
                $hltx_config = $manager->pay_config($config_id, $qd);
                if (!$hltx_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => 'HL支付配置不存在请检查配置'
                    ]);
                }

                $manager->init($hltx_config);
                switch ($ways_type) {
                    case 23001:
                        $pay_channel = 'ALI';
                        break;
                    case 23002:
                        $pay_channel = 'WX';
                        break;
                    case 23004:
                        $pay_channel = 'UPAY';
                        break;
                    default:
                        $pay_channel = 'ALI';
                        break;
                }

                if (!$hltx_merchant || empty($hltx_merchant->mer_no)) {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户未成功开通HL通道!'
                    ]);
                }

                $OutRefundNo = "hltx" . date('YmdHis') . str_pad(rand(0, 9999), 4, 0);
                $hltx_data = [
                    'merNo' => $hltx_merchant->mer_no,
                    'amount' => $refund_amount * 100,
                    'orderNo' => $OutRefundNo,
                    'oriOrderNo' => $out_trade_no,
                    'orderInfo' => '用户退款',
                    'deviceIp' => \EasyWeChat\Kernel\Support\get_client_ip(),
                ];

                $return = $manager->refund_order($hltx_data);
                if ($return['status'] == 1) {
                    $return = $return['data'];
                    if ($return['tradeStatus'] == 'S' || $return['tradeStatus'] == 'R') {
                        //退款请求成功
                        $insert_data = [
                            'status' => 6,
                            'pay_status' => 6,
                            'pay_status_desc' => '已退款',
                            'refund_amount' => $new_refund_amount,
                            'fee_amount' => $new_fee_amount
                        ];
                        $this->update_day_order($insert_data, $out_trade_no);

                        RefundOrder::create([
                            'ways_source' => $order->ways_source,
                            'type' => $ways_type,
                            'refund_amount' => $refund_amount,//退款金额
                            'refund_no' => $OutRefundNo,//退款单号
                            'store_id' => $store_id,
                            'merchant_id' => $merchant_id,
                            'out_trade_no' => $order->out_trade_no,
                            'trade_no' => $order->trade_no
                        ]);
                        RefundSuccessAction::action([
                            'out_trade_no'=>$out_trade_no,
                            'refund_no' => $OutRefundNo, //联动流水号
                        ]);
                        $data = [
                            'refund_amount' => $refund_amount,
                            'out_trade_no' => $out_trade_no,
                            'refund_no' => $OutRefundNo,//退款单号
                            'other_no' => $other_no
                        ];

                        //返佣去掉
                        UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                            'settlement' => '03',
                            'settlement_desc' => '退款订单',
                        ]);
                        MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                            'settlement' => '03',
                            'settlement_desc' => '退款订单',
                        ]);

                        return json_encode([
                            'status' => 1,
                            'message' => '退款成功',
                            'data' => $data,
                        ]);
                    } else {
                        return json_encode([
                            'status' => 2,
                            'message' => '退款失败' . (isset($return['message']) ? $return['message'] : ''),
                        ]);
                    }
                } else {
                    return json_encode($return);
                }
            }

            //长沙银行
            if (25000 < $ways_type && $ways_type < 25999) {
                $config = new ChangshaConfigController();

                $h_merchant = $config->cs_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户号不存在'
                    ]);
                }

                $h_config = $config->cs_config($config_id);
                if (!$h_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '配置不存在请检查配置'
                    ]);
                }

                $obj = new \App\Api\Controllers\Changsha\PayController();
                $data['ECustId'] = $h_merchant->ECustId; //
                $data['OrderId'] = $trade_no; //
                $data['refundAmount'] = $refund_amount; //

                $return = $obj->refund($data);

                //退款请求成功
                if ($return["status"] == 1) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount,//退款金额
                        'refund_no' => $out_trade_no,//退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $out_trade_no, //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $out_trade_no,//退款单号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    $re_data = [
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ];

                    return json_encode($re_data);
                } else {
                    //其他情况
                    $message = $return['message'];

                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }

            //联动优势 退款
            if (4999 < $ways_type && $ways_type < 5999) {
                // file_put_contents('1108.log',var_export($out_trade_no, true)."\r\n",FILE_APPEND);
                // $config = new LinkageConfigController();
                // $linkage_config = $config->linkage_config($config_id);
                // if (!$linkage_config) {
                //     return json_encode([
                //         'status' => '2',
                //         'message' => '联动优势支付配置不存在请检查配置'
                //     ]);
                // }

                // $linkage_merchant = $config->linkage_merchant($store_id, $store_pid);
                // if (!$linkage_merchant) {
                //     return json_encode([
                //         'status' => 2,
                //         'message' => '联动优势支付商户号不存在'
                //     ]);
                // }

                //针对交易当天的成功订单可以进行撤销，过了账期则需要走退费接口。仅支持微信支付宝刷卡交易（即用户被扫）的撤销，其他支付成功订单如需实现相同功能请调用退款接口
                $obj = new \App\Api\Controllers\Linkage\PayController();
                $now_time_start = strtotime(date('Y-m-d 00:00:00', time()));
                $now_time_end = strtotime(date('Y-m-d 23:59:59', time()));
                $order_pay_time = strtotime($order->pay_time);

                $linkage_revoke_data = [
                    'out_trade_no' => $out_trade_no, //订单号
                    'out_refund_no' => time(),
                ];
                
                // file_put_contents('1108.log',var_export($linkage_revoke_data, true)."\r\n",FILE_APPEND);
                
                $return = $obj->refund($linkage_revoke_data); //-1系统错误；0-其他；1-处理成功；2-验签失败
                //成功
                if ($return['status'] == '1') {
                    $insert_data = [
                        'status' => '6',
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    $refund_amount = ($refund_amount / 100); //金额，单位分

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $out_trade_no, //联动流水号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $out_trade_no,
                        'trade_no' => $trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $out_trade_no, //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $out_trade_no, //联动流水号
                        'other_no' => $other_no
                    ];

                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);

                    return json_encode([
                        'status' => '1',
                        'message' => '退款成功',
                        'data' => $data,
                    ]);
                }
                else {
                    //其他情况
                    $message = '退款失败';
                    return json_encode([
                        'status' => '2',
                        'message' => $message
                    ]);
                }
            }

            //威富通 退款
            if (26999 < $ways_type && $ways_type < 27999) {
                $config = new WftPayConfigController();
                $version="v1";
                if(in_array($ways_type,[27101, 27102, 27103, 27104, 27105, 27107])){
                    $version="v2";
                }
                $wftpay_config = $config->wftpay_config($config_id,$version);
                if (!$wftpay_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '威富通支付配置不存在请检查配置'
                    ]);
                }

                $wftpay_merchant = $config->wftpay_merchant($store_id, $store_pid,$version);
                if (!$wftpay_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '威富通支付商户号不存在'
                    ]);
                }

                $wftpay_return_data = [
                    'mch_id' => $wftpay_merchant->mch_id,
                    'out_trade_no' => $out_trade_no,
                    'total_amount' => $total_amount,
                    'refund_fee' => $refund_amount,
                    'private_rsa_key' => $wftpay_config->private_rsa_key,
                    'public_rsa_key' => $wftpay_config->public_rsa_key
                ];
                Log::info('威富通-退款-入参');
                Log::info($wftpay_return_data);
                $obj = new WftPayPayController();
                $return = $obj->refund($wftpay_return_data); //0-系统错误 1-成功 2-退款失败 3-退款处理中 4-退款到银行发现用户的卡作废或者冻结了,导致原路退款银行卡失败
                Log::info('威富通-退款');
                Log::info($return);

                //退款请求成功
                if ($return['status'] == '1') {
                    $refund_count = $return['data']['refund_count']-1; //退款记录数
                    $insert_data = [
                        'status' => '6',
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $return['data']['refund_id_'.$refund_count]??"", //平台退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $out_trade_no,
                        'trade_no' => $trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $return['data']['refund_id_'.$refund_count]??"", //联动流水号
                    ]);
                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单'
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单'
                    ]);

                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['refund_id_'.$refund_count], //平台退款单号
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '1',
                        'message' => '退款成功',
                        'data' => $data
                    ]);
                } //退款处理中
                elseif($return['status'] == '3') {
                    $insert_data = [
                        'status' => '5',
                        'pay_status' => 5,
                        'pay_status_desc' => '退款中'
                    ];
                    $res = $this->update_day_order($insert_data, $out_trade_no);
                    if (!$res) {
                        Log::info('威富通-退款中-更新失败');
                    }

                    $refund_count = $return['data']['refund_count']-1; //退款记录数
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['refund_id_'.$refund_count]??"", //平台退款单号
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '1',
                        'message' => '退款中',
                        'data' => $data
                    ]);
                }
                else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => '2',
                        'message' => $message
                    ]);
                }
            }


            //农商行 退款
            if (20801 == $ways_type || 20802 == $ways_type || 20803 == $ways_type) {
                $config = new NongjinConfigController();
                $h_merchant = $config->nongjin_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '农商行商户号不存在'
                    ]);
                }

                $wftpay_return_data = [
                    'merchantNo' => $h_merchant->merchantNo,
                    'terminalNo' => $h_merchant->terminalNo,
                    'batchNo' => $h_merchant->batchNo,
                    'traceNo' => $h_merchant->batchNo,
                    'mchtRefundNo' => $out_trade_no,
                    'itpOrderId' => $order->trade_no,
//                    'refundAmount' => intval($order->total_amount*100),
                    'refundAmount' => intval($refund_amount*100),
                ];

                $obj = new \App\Api\Controllers\lianfu\PayController();
                $return = $obj->refund1($wftpay_return_data,$h_merchant->signkey);

                //退款请求成功
                if ($return['status'] == '1') {
                    $insert_data = [
                        'status' => '6',
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $return['data']['cposRefundOrderId'], //平台退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $out_trade_no,
                        'trade_no' => $trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $return['data']['cposRefundOrderId']??"", //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['cposRefundOrderId'], //平台退款单号
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '1',
                        'message' => '退款成功',
                        'data' => $data
                    ]);
                } //退款处理中
                elseif($return['status'] == '2') {
                    $insert_data = [
                        'status' => '5',
                        'pay_status' => 5,
                        'pay_status_desc' => '退款中'
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['cposRefundOrderId'],
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '2',
                        'message' => '退款中',
                        'data' => $data
                    ]);
                }
                else {
                    //其他情况
                    $message = '退款失败';
                    return json_encode([
                        'status' => '3',
                        'message' => $message
                    ]);
                }
            }
            //翼支付 退款
            if (30050 == $ways_type || 30051 == $ways_type || 30052 == $ways_type|| 30053 == $ways_type) {
                $config = new WingConfigController();
                $h_merchant = $config->wing_merchant($store_id, $store_pid);

                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '翼支付商户号不存在'
                    ]);
                }

                $wftpay_return_data = [
                    'institutionCode' => $h_merchant->institutionCode,
                    'merchantNo' => $h_merchant->merchantNo,
                    'outTradeNo' => $out_trade_no,
                    'outRequestNo' => 'wingrerefund'.time(),
                    'refundAmt' => $refund_amount>0?$refund_amount*100:intval($order->total_amount*100),
                    'requestDate' => date('Y-m-d H:i:s', time()),
                    'originalTradeDate' => $order->pay_time,
                ];

                $obj = new \App\Api\Controllers\lianfu\PayController();
                $return = $obj->refund2($wftpay_return_data);

                //退款请求成功
                if ($return['status'] == '1') {
                    $insert_data = [
                        'status' => '6',
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $return['data']['outRequestNo'], //平台退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $out_trade_no,
                        'trade_no' => $trade_no
                    ]);
                    //退款成功后的操作
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $refund_no, //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['outRequestNo'], //平台退款单号
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '1',
                        'message' => '退款成功',
                        'data' => $data
                    ]);
                } //退款处理中
                elseif($return['status'] == '2') {
                    $insert_data = [
                        'status' => '5',
                        'pay_status' => 5,
                        'pay_status_desc' => '退款中'
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['outRequestNo'],
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '2',
                        'message' => '退款中',
                        'data' => $data
                    ]);
                }
                else {
                    //其他情况
                    $message = '退款失败';
                    return json_encode([
                        'status' => '3',
                        'message' => $message
                    ]);
                }
            }

            //平安银行 退款
            if (30011 == $ways_type || 30012 == $ways_type || 30013 == $ways_type) {
                $config = new PinganConfigController();
                $h_merchant = $config->pingan_merchant($store_id, $store_pid);

                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '平安银行商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\lianfu\PayController();
                $pingan_data['third_order_id']=$order->trade_no;
                $pingan_data['enterpriseReg']=$h_merchant->enterpriseReg;
                $pingan_data['merchant_id']=$h_merchant->merchant_id;
                $pingan_data['refundMsg']='tkmtk';
                if($refund_amount>0){
                    $pingan_data['refund_amount']=$refund_amount;
                }else{
                    $pingan_data['refund_amount']=$order->total_amount;
                }

                foreach (array_filter($pingan_data) as $k => $v){
                    $Parameters[$k] = $v;
                }
                $buff = "";
                ksort($Parameters);
                foreach ($Parameters as $k => $v) {
                    $buff .= $k . "=" . $v . "&";
                }
                $String = '';
                if (strlen($buff) > 0) {
                    $String = substr($buff, 0, strlen($buff) - 1);
                }
                $String = $String . $h_merchant->appsecret;
                $re=hash('sha256', $String, true);
                $String = strtoupper(bin2hex($re));

                $json='{"appid":"'."$h_merchant->appid".'"'.',"timeStamp":"'.time().'000'.'"'.',"data":'.stripslashes(json_encode($pingan_data,JSON_UNESCAPED_SLASHES)).',"sign":"'.$String.'"'.'}';
                $return = $obj->refund6($json);
                //退款请求成功
                if ($return['status'] == 1) {
                    $insert_data = [
                        'status' => '6',
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $return['data']['refNum'], //平台退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $out_trade_no,
                        'trade_no' => $trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $return['data']['refNum']??"", //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['refNum'], //平台退款单号
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '1',
                        'message' => '退款成功',
                        'data' => $data
                    ]);
                } //退款处理中
                elseif($return['status'] == '2') {
                    $insert_data = [
                        'status' => '5',
                        'pay_status' => 5,
                        'pay_status_desc' => '退款中'
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['outRequestNo'],
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '2',
                        'message' => '退款中',
                        'data' => $data
                    ]);
                }
                else {
                    //其他情况
                    $message = '退款失败';
                    return json_encode([
                        'status' => '3',
                        'message' => $message
                    ]);
                }
            }

            //农行bmp 退款
            if (30001 == $ways_type || 30002 == $ways_type || 30003 == $ways_type) {
                $config = new AbcConfigController();

                $h_merchant = $config->abc_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '农行商户号不存在'
                    ]);
                }

                $obj = new WftpayPayController();
                $data1['trans_type'] = '04';//交易类型
                $data1['mer_id'] = $h_merchant->MerchantId;//key
                $data1['term_id'] = $h_merchant->abczhongduanid;//终端号
                $data1['out_trade_no'] = $out_trade_no;
                $data1['total_amount'] = $order->total_amount;
                $data1['app_id'] = $h_merchant->abckey;//商户号
                $data1['private_rsa_key'] = $h_merchant->private_rsa_key;
                $data1['public_rsa_key'] = $h_merchant->public_rsa_key;
                $return = $obj->refund2($data1);

                //退款请求成功
                if ($return['status'] == 1) {
                    $insert_data = [
                        'status' => '6',
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $return['data']['refNum'], //平台退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $out_trade_no,
                        'trade_no' => $trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $return['data']['refNum']??"", //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['refNum'], //平台退款单号
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '1',
                        'message' => '退款成功',
                        'data' => $data
                    ]);
                } //退款处理中
                elseif($return['status'] == '2') {
                    $insert_data = [
                        'status' => '5',
                        'pay_status' => 5,
                        'pay_status_desc' => '退款中'
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['outRequestNo'],
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '2',
                        'message' => '退款中',
                        'data' => $data
                    ]);
                }
                else {
                    //其他情况
                    $message = '退款失败';
                    return json_encode([
                        'status' => '3',
                        'message' => $message
                    ]);
                }
            }

            //汇旺财 退款
            if (27999 < $ways_type && $ways_type < 28999) {
                $config = new HwcPayConfigController();
                $hwcpay_config = $config->hwcpay_config($config_id);
                if (!$hwcpay_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '汇旺财支付配置不存在请检查配置'
                    ]);
                }

                $hwcpay_merchant = $config->hwcpay_merchant($store_id, $store_pid);
                if (!$hwcpay_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '汇旺财支付商户号不存在'
                    ]);
                }

                $hwcpay_return_data = [
                    'mch_id' => $hwcpay_merchant->mch_id, //门店号
                    'merchant_num' => $hwcpay_merchant->merchant_num, //商户号
                    'out_trade_no' => $out_trade_no,
                    'total_amount' => $total_amount,
                    'refund_fee' => $refund_amount,
                    'private_rsa_key' => $hwcpay_config->private_rsa_key,
                    'public_rsa_key' => $hwcpay_config->public_rsa_key
                ];
//                Log::info('汇旺财-退款-入参');
//                Log::info($hwcpay_return_data);
                $obj = new HwcPayPayController();
                $return = $obj->refund($hwcpay_return_data); //0-系统错误 1-成功 2-退款失败 3-退款处理中 4-退款到银行发现用户的卡作废或者冻结了,导致原路退款银行卡失败
                Log::info('汇旺财-退款');
                Log::info($return);

                //退款请求成功
                if ($return['status'] == '1') {
                    $refund_count = $return['data']['refund_count']-1; //退款记录数
                    $insert_data = [
                        'status' => '6',
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $return['data']['refund_id_'.$refund_count], //平台退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $out_trade_no,
                        'trade_no' => $trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $return['data']['refund_id_'.$refund_count]??"", //联动流水号
                    ]);
                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单'
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单'
                    ]);

                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['refund_id_'.$refund_count], //平台退款单号
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '1',
                        'message' => '退款成功',
                        'data' => $data
                    ]);
                } //退款处理中
                elseif($return['status'] == '3') {
                    $insert_data = [
                        'status' => '5',
                        'pay_status' => 5,
                        'pay_status_desc' => '退款中'
                    ];
                    $res = $this->update_day_order($insert_data, $out_trade_no);
                    if (!$res) {
                        Log::info('汇旺财-退款中-更新失败');
                    }

                    $refund_count = $return['data']['refund_count']-1; //退款记录数
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['refund_id_'.$refund_count], //平台退款单号
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '1',
                        'message' => '退款中',
                        'data' => $data
                    ]);
                }
                else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => '2',
                        'message' => $message
                    ]);
                }

            }
            
             //银卡 退款
            if (38000 < $ways_type && $ways_type < 38010) {
                $ykpay_return_data = [
                    'out_trade_no' => $out_trade_no,
                    'total_amount' => $total_amount,
                    'refund_fee' => $refund_amount,
                ];
                $return = (new \App\Api\Controllers\Yinka\PayController($store_id))->refund($ykpay_return_data);

                //退款请求成功
                if ($return['status'] == '1') {
                    $insert_data = [
                        'status' => '6',
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $refund_no, //平台退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $out_trade_no,
                        'trade_no' => $trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $refund_no??"", //联动流水号
                    ]);
                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单'
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单'
                    ]);

                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $refund_no, //平台退款单号
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '1',
                        'message' => '退款成功',
                        'data' => $data
                    ]);
                } //退款处理中
                elseif($return['status'] == '3') {
                    $insert_data = [
                        'status' => '5',
                        'pay_status' => 5,
                        'pay_status_desc' => '退款中'
                    ];
                    $res = $this->update_day_order($insert_data, $out_trade_no);
                    if (!$res) {
                        Log::info('汇旺财-退款中-更新失败');
                    }

                    $refund_count = $return['data']['refund_count']-1; //退款记录数
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['refund_id_'.$refund_count], //平台退款单号
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '1',
                        'message' => '退款中',
                        'data' => $data
                    ]);
                }
                else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => '2',
                        'message' => $message
                    ]);
                }

            }
            
            //重庆银行 退款
            if (44000 < $ways_type && $ways_type < 44010) {
                $params = [
                    'config_id' => $config_id,
                    'store_id' => $store_id,
                    'store_pid' => $store_pid,
                    'out_trade_no' => $out_trade_no,
                    'refund_no' => $refund_no,
                    'refund_amount' => $refund_amount,
                    'new_refund_amount' => $new_refund_amount,
                    'new_fee_amount' => $new_fee_amount,
                    'ways_type' => $ways_type,
                    'merchant_id' => $merchant_id,
                    'trade_no' => $trade_no
                ];
                return (new \App\Api\Controllers\Cqcbank\LogicController())->refundParams($params, $order);
            }

            //民生银行 退款
            if (50001 == $ways_type || 50002 == $ways_type || 50003 == $ways_type) {
                $params = [
                    'config_id' => $config_id,
                    'store_id' => $store_id,
                    'store_pid' => $store_pid,
                    'out_trade_no' => $out_trade_no,
                    'refund_no' => $refund_no,
                    'refund_amount' => $refund_amount,
                    'new_refund_amount' => $new_refund_amount,
                    'new_fee_amount' => $new_fee_amount,
                    'ways_type' => $ways_type,
                    'merchant_id' => $merchant_id,
                    'trade_no' => $trade_no
                ];
                return (new \App\Api\Controllers\Cmbc\LogicController())->refundParams($params, $order);
            }


            //兴业银行 退款
            if (39000 < $ways_type && $ways_type < 39010) {
                $config = new \App\Api\Controllers\Config\CibPayConfigController();
                $cibpay_config = $config->wftpay_config($config_id);
                if (!$cibpay_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '威富通支付配置不存在请检查配置'
                    ]);
                }

                $cibpay_merchant = $config->cib_merchant($store_id, $store_pid);
                if (!$cibpay_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '威富通支付商户号不存在'
                    ]);
                }

                $cibpay_return_data = [
                    'mch_id' => $cibpay_merchant->mch_id,
                    'out_trade_no' => $out_trade_no,
                    'total_amount' => $total_amount,
                    'refund_fee' => $refund_amount,
                    'private_rsa_key' => $cibpay_config->private_rsa_key,
                    'public_rsa_key' => $cibpay_config->public_rsa_key
                ];

                $obj = new WftPayPayController();
                $return = $obj->refund($cibpay_return_data); //0-系统错误 1-成功 2-退款失败 3-退款处理中 4-退款到银行发现用户的卡作废或者冻结了,导致原路退款银行卡失败

                //退款请求成功
                if ($return['status'] == '1') {
                    $refund_count = $return['data']['refund_count']-1; //退款记录数
                    $insert_data = [
                        'status' => '6',
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);

                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $return['data']['refund_id_'.$refund_count]??"", //平台退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $out_trade_no,
                        'trade_no' => $trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $return['data']['refund_id_'.$refund_count]??"", //联动流水号
                    ]);
                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单'
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单'
                    ]);

                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['refund_id_'.$refund_count]??"", //平台退款单号
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '1',
                        'message' => '退款成功',
                        'data' => $data
                    ]);
                } //退款处理中
                elseif($return['status'] == '3') {
                    $insert_data = [
                        'status' => '5',
                        'pay_status' => 5,
                        'pay_status_desc' => '退款中'
                    ];
                    $res = $this->update_day_order($insert_data, $out_trade_no);
                    if (!$res) {
                        Log::info('威富通-退款中-更新失败');
                    }
                    $refund_count = ($return['data']['refund_count']??1)-1; //退款记录数
                    RefundOrderDoing::create([
                        "company"=>$order->company,
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $return['data']['refund_id_'.$refund_count]??"", //平台退款单号
                        "other_refund_no"=> $return['data']['refund_id_'.$refund_count]??"",
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $out_trade_no,
                        'trade_no' => $trade_no,
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $return['data']['refund_id_'.$refund_count]??"", //平台退款单号
                        'other_no' => $other_no
                    ];
                    return json_encode([
                        'status' => '1',
                        'message' => '退款中',
                        'data' => $data
                    ]);
                }
                else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => '2',
                        'message' => $message
                    ]);
                }
            }
            
            //TODO::银盛退款
            if (in_array($ways_type, [14001, 14002, 14004])) {
                $manager = new \App\Api\Controllers\Config\YinshengConfigController();
                $yinsheng_config = $manager->yinsheng_config('1234');
                if (!$yinsheng_config) {
                    return json_encode([
                        'status' => '2',
                        'message' => '银盛支付不存在,请检查配置'
                    ]);
                }
                $yinsheng_merchant = $manager->yinsheng_merchant($store_id, $store_pid);
                if (!$yinsheng_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '银盛支付,商户号未配置'
                    ]);
                }
                $yinsheng_obj = new \App\Api\Controllers\YinSheng\PayController();
                $return = $yinsheng_obj->refund([
                    'refund_no' => $refund_no,
                    'refund_amount' => $refund_amount,
                    'out_trade_no' => $out_trade_no,
                    'config_id' => $yinsheng_merchant->config_id,
                    'store_id' => $yinsheng_merchant->store_id,
                    'partner_id' => $yinsheng_merchant->partner_id,
                    'seller_id' => $yinsheng_merchant->seller_id,
                    'seller_name' => $yinsheng_merchant->seller_name,
                    'public_key' => $yinsheng_config->public_key,
                    'private_key' => $yinsheng_config->private_key,
                    'pfx_password' => $yinsheng_config->pfx_password,
                    'business_code' => $yinsheng_merchant->business_code
                ]);
                if ($return["status"] == 1) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);
                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $out_trade_no, //退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $out_trade_no??"", //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $out_trade_no, //退款单号
                        'other_no' => $other_no
                    ];
                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    $re_data = [
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ];
                    return json_encode($re_data);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }

            //东莞银行 退款
            if (in_array($ways_type, [40001, 40002, 40003])){
                $DongGuanConfig = (new \App\Api\Controllers\Config\Dong_GuanConfigController())->getConfig($store_id, $store_pid);
                if (!$DongGuanConfig) {
                    return json_encode([
                        'status' => '2',
                        'message' => '商户未配置东莞银行'
                    ]);
                }
                $return = (new \App\Api\Controllers\Dong_Guan\PayController())->refund([
                    'refund_no' => $refund_no,
                    'refund_amount' => $refund_amount,
                    'out_trade_no' => $out_trade_no,
                    'total_amount' => $order->total_amount,
                    'mch_id' => $DongGuanConfig['mch_id'],
                    'private_key' => $DongGuanConfig['private_key'],
                    'op_device_id' => $DongGuanConfig['op_device_id'],
                ]);
                if ($return["status"] == 1) {
                    $insert_data = [
                        'status' => 6,
                        'pay_status' => 6,
                        'pay_status_desc' => '已退款',
                        'refund_amount' => $new_refund_amount,
                        'fee_amount' => $new_fee_amount
                    ];
                    $this->update_day_order($insert_data, $out_trade_no);
                    RefundOrder::create([
                        'ways_source' => $order->ways_source,
                        'type' => $ways_type,
                        'refund_amount' => $refund_amount, //退款金额
                        'refund_no' => $refund_no, //退款单号
                        'store_id' => $store_id,
                        'merchant_id' => $merchant_id,
                        'out_trade_no' => $order->out_trade_no,
                        'trade_no' => $order->trade_no
                    ]);
                    RefundSuccessAction::action([
                        'out_trade_no'=>$out_trade_no,
                        'refund_no' => $refund_no??"", //联动流水号
                    ]);
                    $data = [
                        'refund_amount' => $refund_amount,
                        'out_trade_no' => $out_trade_no,
                        'refund_no' => $refund_no, //退款单号
                        'other_no' => $other_no
                    ];
                    //返佣去掉
                    UserWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    MerchantWalletDetail::where('out_trade_no', $out_trade_no)->update([
                        'settlement' => '03',
                        'settlement_desc' => '退款订单',
                    ]);
                    $re_data = [
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => $data,
                    ];
                    return json_encode($re_data);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 2,
                        'message' => $message
                    ]);
                }
            }

            if (isset($ways_type)) {
                $params = [
                    'config_id' => $config_id,
                    'store_id' => $store_id,
                    'store_pid' => $store_pid,
                    'out_trade_no' => $out_trade_no,
                    'refund_no' => $refund_no,
                    'refund_amount' => $refund_amount,
                    'new_refund_amount' => $new_refund_amount,
                    'new_fee_amount' => $new_fee_amount,
                    'ways_type' => $ways_type,
                    'merchant_id' => $merchant_id,
                    'trade_no' => $trade_no
                ];
                return (new \App\Api\Controllers\DevicePay\ScanpayController())->refund($params, $order);
            }

            $re_data = [
                'status' => 2,
                'message' => '退款失败',
            ];

            return json_encode($re_data);
        } catch (\Exception $ex) {
            // Log::info('商户端-退款');
            // Log::info($ex->getMessage() .' | '.$ex->getFile().' | '. $ex->getLine());
            return json_encode([
                'status' => '-1',
                'message' => $ex->getMessage() .' | '.$ex->getFile().' | '. $ex->getLine()
            ]);
        } finally {
            if(!empty($data['out_trade_no'])){
                LockUtil::unLock("out_trade_no:".$data['out_trade_no']??"","REFUND");
            }
        }
    }

    public function new_order_count (Request $request)
    {
        try {
            $merchant = $this->parseToken();
            $merchant_id = $request->get('merchant_id');
            $time_start_s = date('Y-m-d 00:00:00', time());
            $time_start_e = date('Y-m-d 23:59:59', time());
            $time_start = $request->get('time_start', $time_start_s); //开始时间
            $time_end = $request->get('time_end', $time_start_e); //结束时间
            $store_id = $request->get('store_id', '');
            $data = [];
            if (!$merchant_id) {
                $merchant_id = $merchant->merchant_id;
            }
            $params = [
                'key' => 'Fv5xDRRUfS',
                'merchant_id' => $merchant_id,
                'time_start' => $time_start,
                'time_end' => $time_end,
                'store_id' => $store_id
            ];
            $params = http_build_query($params);
            $curl = curl_init();
            $url = 'http://center.5ft.wiki/api/bill/order_count?'.$params;
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, []);
            $content = curl_exec($curl);
            curl_close($curl);
            $content = json_decode($content, true);
            if (isset($content['code']) && $content['code'] == 1) {
                $data = $content['data'];
                $this->message = '数据返回成功';
            } else {
                $this->message = $content['msg'].' url:'.$url;
            }
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }

    //对账统计-比较全-实时
    public function order_count(Request $request)
    {
        // dump(2);die;
        try {
            $merchant = $this->parseToken();
            $time_start_s = date('Y-m-d 00:00:00', time());
            $time_start_e = date('Y-m-d 23:59:59', time());
            $store_id = $request->get('store_id', '');
            $merchant_id = $request->get('merchant_id', '');
            $ways_source = $request->get('ways_source', '');
            $time_start = $request->get('time_start', $time_start_s); //开始时间
            $time_end = $request->get('time_end', $time_start_e); //结束时间
            $time_type = $request->get('time_type', '2');
            $return_type = $request->get('return_type', '01');
            $pay_status = $request->get('pay_status', '');
            $device_id = $request->get('device_id', '');

            $day = date('Ymd', strtotime($time_start));
            $table = 'orders_' . $day;

            $check_data = [
                'time_start' => '开始时间',
                'time_end' => '结束时间',
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }
            
            if($time_start>=$time_end){
                return json_encode([
                    'status' => 2,
                    'message' => '开始时间不能大于结束时间'
                ]);
            }

            $where = [];
            $where13 = [];
            $whereIn = [];
            $store_ids = [];
            if ($store_id) {
                $where[] = ['store_id', '=', $store_id];
                $store_ids = [
                    [
                        'store_id' => $store_id,
                    ]
                ];
            } else {
                $MerchantStore = MerchantStore::where('merchant_id', $merchant->merchant_id)
                    ->select('store_id')
                    ->get();

                if (!$MerchantStore->isEmpty()) {
                    $store_ids = $MerchantStore->toArray();
                }
            }


            //收银员
            if ($merchant->merchant_type == 2) {
                $where[] = ['merchant_id', '=', $merchant->merchant_id];
            }

            if ($device_id) {
                $where[] = ['device_id', '=',$device_id];
            }

            //是否传收银员ID
            if ($request->get('merchant_id', '')) {
                $where[] = ['merchant_id', '=', $request->get('merchant_id', '')];
            }

            if ($time_type == "1") {
                $y = date("Y", time());
                $time_start = $y . $time_start;
                $time_end = $y . $time_end;

                $day = date('Ymd', strtotime($time_start));
                $table = 'orders_' . $day;
            }

            if ($time_start) {
                if ($time_type == "1") {
                    $time_start = date('Y-m-d 00:00:00', strtotime($time_start));
                } else {
                    $time_start = date('Y-m-d H:i:s', strtotime($time_start));
                }

                $where[] = ['created_at', '>=', $time_start];
            }

            if ($time_end) {
                if ($time_type == "1") {
                    $time_end = date('Y-m-d 23:59:59', strtotime($time_end));
                } else {
                    $time_end = date('Y-m-d H:i:s', strtotime($time_end));
                }

                $where[] = ['created_at', '<=', $time_end];
            }

            //区间
            $e_order = '0.00';
            $store_data=DB::table('stores')->where('store_id', $store_ids[0]['store_id'])->first();
            //限制时间
            $date = (strtotime($time_end) - strtotime($time_start)) / 86400;
            $day = 31;
            $now_time = date('Y-m-d H:i:s', time());
            $end = date('Y-m-d 22:00:00', time());
            if ($now_time > $end) {
                $day = 31;
            }

            if ($date > $day) {
                $time_start_s = date('Y-m-d 00:00:00', time());
                $time_start_e = date('Y-m-d 23:59:59', time());

                $time_start = $time_start_s;
                $time_end = $time_start_e;
            }

            //跨天操作
            $time_start_db = date('Ymd', strtotime($time_start));
            $time_end_db = date('Ymd', strtotime($time_end));
            $is_ct_time = 0;
            if ($time_start_db != $time_end_db) {
                $is_ct_time = 1;
            }

            if (env('DB_D1_HOST')) {
                if ($is_ct_time) {
                    if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                        $obj = DB::connection("mysql_d1")->table('order_items');
                    } else {
                        $obj = DB::connection("mysql_d1")->table('orders');
                    }
                } else {
                    if (Schema::hasTable($table)) {
                        $obj = DB::connection("mysql_d1")->table($table);
                    } else {
                        if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                            $obj = DB::connection("mysql_d1")->table('order_items');
                        } else {
                            $obj = DB::connection("mysql_d1")->table('orders');
                        }
                    }
                }
            } else {
                if ($is_ct_time) {
                    if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                        $obj = DB::table('order_items');
                    } else {
                        $obj = DB::table('orders');
                    }
                } else {
                    if (Schema::hasTable($table)) {
                        $obj = DB::table($table);
                    } else {
                        if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                            $obj = DB::table('order_items');
                        } else {
                            $obj = DB::table('orders');
                        }
                    }
                }
            }

            if (env('DB_D1_HOST')) {
                if ($is_ct_time) {
                    if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                        $obj1 = DB::connection("mysql_d1")->table('order_items');
                    } else {
                        $obj1 = DB::connection("mysql_d1")->table('orders');
                    }
                } else {
                    if (Schema::hasTable($table)) {
                        $obj1 = DB::connection("mysql_d1")->table($table);
                    } else {
                        if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                            $obj1 = DB::connection("mysql_d1")->table('order_items');
                        } else {
                            $obj1 = DB::connection("mysql_d1")->table('orders');
                        }
                    }
                }
            } else {
                if ($is_ct_time) {
                    if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                        $obj1 = DB::table('order_items');
                    } else {
                        $obj1 = DB::table('orders');
                    }
                } else {
                    if (Schema::hasTable($table)) {
                        $obj1 = DB::table($table);
                    } else {
                        if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                            $obj1 = DB::table('order_items');
                        } else {
                            $obj1 = DB::table('orders');
                        }
                    }
                }
            }

            if (env('DB_D1_HOST')) {
                if ($is_ct_time) {
                    if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                        $obj2 = DB::connection("mysql_d1")->table('order_items');
                    } else {
                        $obj2 = DB::connection("mysql_d1")->table('orders');
                    }
                } else {
                    if (Schema::hasTable($table)) {
                        $obj2 = DB::connection("mysql_d1")->table($table);
                    } else {
                        if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                            $obj2 = DB::connection("mysql_d1")->table('order_items');
                        } else {
                            $obj2 = DB::connection("mysql_d1")->table('orders');
                        }
                    }
                }
            } else {
                if ($is_ct_time) {
                    if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                        $obj2 = DB::table('order_items');
                    } else {
                        $obj2 = DB::table('orders');
                    }
                } else {
                    if (Schema::hasTable($table)) {
                        $obj2 = DB::table($table);
                    } else {
                        if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                            $obj2 = DB::table('order_items');
                        } else {
                            $obj2 = DB::table('orders');
                        }
                    }
                }
            }

            if (env('DB_D1_HOST')) {
                if ($is_ct_time) {
                    if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                        $obj3 = DB::connection("mysql_d1")->table('order_items');
                    } else {
                        $obj3 = DB::connection("mysql_d1")->table('orders');
                    }
                } else {
                    if (Schema::hasTable($table)) {
                        $obj3 = DB::connection("mysql_d1")->table($table);
                    } else {
                        if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                            $obj3 = DB::connection("mysql_d1")->table('order_items');
                        } else {
                            $obj3 = DB::connection("mysql_d1")->table('orders');
                        }
                    }
                }
            } else {
                if ($is_ct_time) {
                    if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                        $obj3 = DB::table('order_items');
                    } else {
                        $obj3 = DB::table('orders');
                    }
                } else {
                    if (Schema::hasTable($table)) {
                        $obj3 = DB::table($table);
                    } else {
                        if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                            $obj3 = DB::table('order_items');
                        } else {
                            $obj3 = DB::table('orders');
                        }
                    }
                }
            }

            if (env('DB_D1_HOST')) {
                if ($is_ct_time) {
                    if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                        $obj4 = DB::connection("mysql_d1")->table('order_items');
                    } else {
                        $obj4 = DB::connection("mysql_d1")->table('orders');
                    }
                } else {
                    if (Schema::hasTable($table)) {
                        $obj4 = DB::connection("mysql_d1")->table($table);
                    } else {
                        if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                            $obj4 = DB::connection("mysql_d1")->table('order_items');
                        } else {
                            $obj4 = DB::connection("mysql_d1")->table('orders');
                        }
                    }
                }
            } else {
                if ($is_ct_time) {
                    if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                        $obj4 = DB::table('order_items');
                    } else {
                        $obj4 = DB::table('orders');
                    }
                } else {
                    if (Schema::hasTable($table)) {
                        $obj4 = DB::table($table);
                    } else {
                        if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                            $obj4 = DB::table('order_items');
                        } else {
                            $obj4 = DB::table('orders');
                        }
                    }
                }
            }

            if (env('DB_D1_HOST')) {
                if ($is_ct_time) {
                    if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                        $obj5 = DB::connection("mysql_d1")->table('order_items');
                    } else {
                        $obj5 = DB::connection("mysql_d1")->table('orders');
                    }
                } else {
                    if (Schema::hasTable($table)) {
                        $obj5 = DB::connection("mysql_d1")->table($table);
                    } else {
                        if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                            $obj5 = DB::connection("mysql_d1")->table('order_items');
                        } else {
                            $obj5 = DB::connection("mysql_d1")->table('orders');
                        }
                    }
                }
            } else {
                if ($is_ct_time) {
                    if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                        $obj5 = DB::table('order_items');
                    } else {
                        $obj5 = DB::table('orders');
                    }
                } else {
                    if (Schema::hasTable($table)) {
                        $obj5 = DB::table($table);
                    } else {
                        if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                            $obj5 = DB::table('order_items');
                        } else {
                            $obj5 = DB::table('orders');
                        }
                    }
                }
            }
            if ($merchant_id) {
                $order_data = $obj->where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
                    ->where('merchant_id', $merchant_id)
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $refund_obj = Order::where('merchant_id', $merchant_id)
                    ->where($where)
                    ->where('pay_status', 6) //退款
                    ->select('refund_amount');

                $discount_obj = Order::where('merchant_id', $merchant_id)
                    ->where($where)
                    ->where('discount_amount', '>', 0) //优惠
                    ->select('discount_amount');

                //会员卡
                $member_data = Order::where('merchant_id', $merchant_id)
                    ->where($where)
                    ->where('pay_status', 1)
                    ->where('ways_type', '18888')
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount', 'member_money');

                $member_refund_obj = Order::whereIn('store_id', $store_ids)
                    ->where($where)
                    ->where('pay_status', 6)
                    ->where('ways_type', '18888')
                    ->select('refund_amount');

                //支付宝
                $alipay_order_data = $obj1->where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
                    ->where('ways_source', 'alipay')
                    ->where('merchant_id', $merchant_id)
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $alipay_refund_obj = Order::where('merchant_id', $merchant_id)
                    ->where('ways_source', 'alipay')
                    ->where($where)
                    ->where('pay_status', 6)
                    ->select('refund_amount');

                //支付宝刷脸
                $alipay_face_order_data = Order::where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
                    ->where('ways_source', 'alipay')
                    ->where('merchant_id', $merchant_id)
                    ->where('pay_method', 'alipay_face') //支付宝刷脸
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $alipay_face_refund_obj = Order::where('merchant_id', $merchant_id)
                    ->where('ways_source', 'alipay')
                    ->where($where)
                    ->where('pay_status', 6)
                    ->where('pay_method', 'alipay_face') //支付宝刷脸
                    ->select('refund_amount');

                //微信
                $weixin_order_data = $obj2->where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
                    ->where('ways_source', 'weixin')
                    ->where('merchant_id', $merchant_id)
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $weixin_refund_obj = Order::where('merchant_id', $merchant_id)
                    ->where('ways_source', 'weixin')
                    ->where($where)
                    ->where('pay_status', 6)
                    ->select('refund_amount');

                //微信刷脸
                $weixin_face_order_data = Order::where('merchant_id', $merchant_id)
                    ->where('ways_source', 'weixin')
                    ->where('pay_method', 'weixin_face') //微信刷脸
                    ->where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $weixin_face_refund_obj = Order::where('merchant_id', $merchant_id)
                    ->where('ways_source', 'weixin')
                    ->where($where)
                    ->where('pay_status', 6)
                    ->where('pay_method', 'weixin_face') //微信刷脸
                    ->select('refund_amount');

                //京东
                $jd_order_data = $obj3->where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
//                    ->where('ways_source', 'jd')
                    ->whereIn('ways_type', [15001, 15002, 15003, 15004, 15005]) //哆啦宝
                    ->where('merchant_id', $merchant_id)
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $jd_refund_obj = Order::where('merchant_id', $merchant_id)
//                    ->where('ways_source', 'jd')
                    ->whereIn('ways_type', [15001, 15002, 15003, 15004, 15005]) //哆啦宝
                    ->where($where)
                    ->where('pay_status', 6)
                    ->select('refund_amount');

                //银联刷卡
                $un_order_data = $obj4->where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
                    ->whereIn('ways_type', [6005, 8005, 13005, 15005]) //新大陆+京东刷卡
                    ->where('ways_source', 'unionpay')
                    ->where('merchant_id', $merchant_id)
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $un_refund_obj = Order::where('merchant_id', $merchant_id)
                    ->whereIn('ways_type', [6005, 8005, 13005, 15005]) //新大陆+京东刷卡
                    ->where('ways_source', 'unionpay')
                    ->where($where)
                    ->where('pay_status', 6)
                    ->select('refund_amount');

                //银联扫码
                $unqr_order_data = $obj5->where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
                    ->whereNotIn('ways_type', [6004, 8004, 13004, 15004]) //新大陆+云闪付
                    ->where('ways_source', 'unionpay')
                    ->where('merchant_id', $merchant_id)
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $unqr_refund_obj = Order::where('merchant_id', $merchant_id)
                    ->whereNotIn('ways_type', [6004, 8004, 13004, 15004]) //新大陆+京东刷卡
                    ->where('ways_source', 'unionpay')
                    ->where($where)
                    ->where('pay_status', 6)
                    ->select('refund_amount');
            } else {
                $order_data = Order::whereIn('store_id', $store_ids)
                    ->where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $refund_obj = Order::whereIn('store_id', $store_ids)
                    ->where($where)
                    ->where('pay_status', 6) //退款
                    ->select('refund_amount');

                //会员卡支付
                $member_data = Order::whereIn('store_id', $store_ids)
                    ->where($where)
                    ->where('pay_status', 1)
                    ->where('ways_type', '18888')
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount', 'member_money');

                $member_refund_obj = Order::whereIn('store_id', $store_ids)
                    ->where($where)
                    ->where('pay_status', 6)
                    ->where('ways_type', '18888')
                    ->select('refund_amount');

                //支付宝
                $alipay_order_data = $obj1->whereIn('store_id', $store_ids)
                    ->where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
                    ->where('ways_source', 'alipay')
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $alipay_refund_obj = Order::whereIn('store_id', $store_ids)
                    ->where($where)
                    ->where('pay_status', 6) //退款
                    ->where('ways_source', 'alipay')
                    ->select('refund_amount');

                //支付宝刷脸
                $alipay_face_order_data = Order::whereIn('store_id', $store_ids)
                    ->where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
                    ->where('ways_source', 'alipay')
                    ->where('pay_method', 'alipay_face') //支付宝刷脸
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $alipay_face_refund_obj = Order::whereIn('store_id', $store_ids)
                    ->where($where)
                    ->where('pay_status', 6) //退款
                    ->where('ways_source', 'alipay')
                    ->where('pay_method', 'alipay_face') //支付宝刷脸
                    ->select('refund_amount');

                //微信
                $weixin_order_data = $obj2->whereIn('store_id', $store_ids)
                    ->where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
                    ->where('ways_source', 'weixin')
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $weixin_refund_obj = Order::whereIn('store_id', $store_ids)
                    ->where($where)
                    ->where('pay_status', 6)
                    ->where('ways_source', 'weixin')
                    ->select('refund_amount');

                //微信刷脸
                $weixin_face_order_data = Order::whereIn('store_id', $store_ids)
                    ->where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
                    ->where('ways_source', 'weixin')
                    ->where('pay_method', 'weixin_face') //微信刷脸
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $weixin_face_refund_obj = Order::whereIn('store_id', $store_ids)
                    ->where($where)
                    ->where('pay_status', 6) //退款
                    ->where('ways_source', 'weixin')
                    ->where('pay_method', 'weixin_face') //微信刷脸
                    ->select('refund_amount');

                //京东
                $jd_order_data = $obj3->whereIn('store_id', $store_ids)
                    ->where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
//                    ->where('ways_source', 'jd')
                    ->whereIn('ways_type', [15001, 15002, 15003, 15004, 15005]) //哆啦宝
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $jd_refund_obj = Order::whereIn('store_id', $store_ids)
                    ->where($where)
                    ->where('pay_status', 6)
//                    ->where('ways_source', 'jd')
                    ->whereIn('ways_type', [15001, 15002, 15003, 15004, 15005]) //哆啦宝
                    ->select('refund_amount');

                //银联刷卡
                $un_order_data = $obj4->whereIn('store_id', $store_ids)
                    ->where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
                    ->whereIn('ways_type', [6005, 8005, 13005]) //新大陆+京东刷卡
                    ->where('ways_source', 'unionpay')
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $un_refund_obj = Order::whereIn('store_id', $store_ids)
                    ->where($where)
                    ->where('pay_status', 6)
                    ->where('ways_source', 'unionpay')
                    ->whereIn('ways_type', [6005, 8005, 13005]) //新大陆+京东刷卡
                    ->select('refund_amount');

                //银联二维码
                $unqr_order_data = $obj5->whereIn('store_id', $store_ids)
                    ->where($where)
                    ->whereIn('pay_status', [1, 6]) //成功+退款
                    ->whereNotIn('ways_type', [6004, 8004, 13004, 15004]) //新大陆+云闪付
                    ->where('ways_source', 'unionpay')
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $unqr_refund_obj = Order::whereIn('store_id', $store_ids)
                    ->where($where)
                    ->where('pay_status', 6)
                    ->where('ways_source', 'unionpay')
                    ->whereNotIn('ways_type', [6004, 8004, 13004, 15004]) //去除新大陆+京东刷卡
                    ->select('refund_amount');

                $discount_obj = Order::whereIn('store_id', $store_ids)
                    ->where($where)
                    ->where('discount_amount', '>', 0) //优惠
                    ->select('discount_amount');
            }
            $rata_data = $obj->whereIn('store_id', $store_ids)
                ->where($where)
                ->whereIn('pay_status', [1,6])
                ->select('rata_money');
            $store_money=DB::table('stores')->where('store_id', $store_ids[0]['store_id'])->first();
            //总的
            $total_amount = $order_data->sum('total_amount'); //交易金额
            $rata_fee = $rata_data->sum('rata_money'); //今日服务费
            $yrata_fee = 0;//昨日服务费
            $refund_amount = $refund_obj->sum('refund_amount'); //退款金额
            $discount_amount = $discount_obj->sum('discount_amount');//优惠金额
            $discount_count=count($discount_obj->get());
            $fee_amount = $order_data->sum('fee_amount'); //结算服务费/手续费
            $mdiscount_amount = $order_data->sum('mdiscount_amount'); //商家优惠金额
            $total_member_amount = $member_data->sum('total_amount'); //会员卡支付金额
            // $total_amount=$total_amount+$refund_amount;
            $get_amount = $total_amount - $refund_amount - $mdiscount_amount - $total_member_amount; //商家实收，交易金额-退款金额-会员支付金额
            $receipt_amount = $get_amount - $fee_amount; //实际净额，实收-手续费
            $e_order = '' . $e_order . '';
            $total_count = '' . count($order_data->get()) . '';
            $refund_count = count($refund_obj->get());

            //支付宝
            $alipay_total_amount = $alipay_order_data->sum('total_amount'); //交易金额
            $alipay_refund_amount = $alipay_refund_obj->sum('refund_amount'); //退款金额
            $alipay_fee_amount = $alipay_order_data->sum('fee_amount'); //结算服务费/手续费
            $alipay_mdiscount_amount = $alipay_order_data->sum('mdiscount_amount'); //商家优惠金额
            $alipay_get_amount = $alipay_total_amount - $alipay_refund_amount - $alipay_mdiscount_amount; //商家实收，交易金额-退款金额
            $alipay_receipt_amount = $alipay_get_amount - $alipay_fee_amount; //实际净额，实收-手续费
            $alipay_total_count = '' . count($alipay_order_data->get()) . '';
            $alipay_refund_count = count($alipay_refund_obj->get());

            //微信
            $weixin_total_amount = $weixin_order_data->sum('total_amount'); //交易金额
            $weixin_refund_amount = $weixin_refund_obj->sum('refund_amount'); //退款金额
            $weixin_fee_amount = $weixin_order_data->sum('fee_amount'); //结算服务费/手续费
            $weixin_mdiscount_amount = $weixin_order_data->sum('mdiscount_amount'); //商家优惠金额
            $weixin_get_amount = $weixin_total_amount - $weixin_refund_amount - $weixin_mdiscount_amount; //商家实收，交易金额-退款金额
            $weixin_receipt_amount = $weixin_get_amount - $weixin_fee_amount; //实际净额，实收-手续费
            $weixin_total_count = '' . count($weixin_order_data->get()) . '';
            $weixin_refund_count = count($weixin_refund_obj->get());

            //京东
            $jd_total_amount = $jd_order_data->sum('total_amount'); //交易金额
            $jd_refund_amount = $jd_refund_obj->sum('refund_amount'); //退款金额
            $jd_fee_amount = $jd_order_data->sum('fee_amount'); //结算服务费/手续费
            $jd_mdiscount_amount = $jd_order_data->sum('mdiscount_amount'); //商家优惠金额
            $jd_get_amount = $jd_total_amount - $jd_refund_amount - $jd_mdiscount_amount; //商家实收，交易金额-退款金额
            $jd_receipt_amount = $jd_get_amount - $jd_fee_amount; //实际净额，实收-手续费
            $jd_total_count = '' . count($jd_order_data->get()) . '';
            $jd_refund_count = count($jd_refund_obj->get());

            //银联刷卡
            $un_total_amount = $un_order_data->sum('total_amount'); //交易金额
            $un_refund_amount = $un_refund_obj->sum('refund_amount'); //退款金额
            $un_fee_amount = $un_order_data->sum('fee_amount'); //结算服务费/手续费
            $un_mdiscount_amount = $un_order_data->sum('mdiscount_amount'); //商家优惠金额
            $un_get_amount = $un_total_amount - $un_refund_amount - $un_mdiscount_amount; //商家实收，交易金额-退款金额
            $un_receipt_amount = $un_get_amount - $un_fee_amount; //实际净额，实收-手续费
            $un_total_count = '' . count($un_order_data->get()) . '';
            $un_refund_count = count($un_refund_obj->get());

            //银联扫码
            $unqr_total_amount = $unqr_order_data->sum('total_amount'); //交易金额
            $unqr_refund_amount = $unqr_refund_obj->sum('refund_amount'); //退款金额
            $unqr_fee_amount = $unqr_order_data->sum('fee_amount'); //结算服务费/手续费
            $unqr_mdiscount_amount = $unqr_order_data->sum('mdiscount_amount'); //商家优惠金额
            $unqr_get_amount = $unqr_total_amount - $unqr_refund_amount - $unqr_mdiscount_amount; //商家实收，交易金额-退款金额
            $unqr_receipt_amount = $unqr_get_amount - $unqr_fee_amount; //实际净额，实收-手续费
            $unqr_total_count = '' . count($unqr_order_data->get()) . '';
            $unqr_refund_count = count($unqr_refund_obj->get());

            //会员卡支付
            $member_total_amount = $member_data->sum('total_amount'); //交易金额
            $member_deduction_sum = $member_data->sum('member_money'); //会员支付金额
            $member_refund_amount = $member_refund_obj->sum('total_amount'); //退款金额
            $member_fee_amount = $member_data->sum('fee_amount'); //结算服务费/手续费
            $member_mdiscount_amount = $member_data->sum('mdiscount_amount'); //商家优惠金额
            $member_get_amount = $member_total_amount - $member_refund_amount - $member_mdiscount_amount; //商家实收，交易金额-退款金额
            $member_receipt_amount = $member_get_amount - $member_fee_amount; //实际净额，实收-手续费
            $member_total_count = '' . count($member_data->get()) . '';
            $member_refund_count = count($member_refund_obj->get());
            $member_total_deduction = $member_total_amount - $member_deduction_sum; //

            //支付宝刷脸
            $alipay_face_total_amount = $alipay_face_order_data->sum('total_amount'); //交易金额
            $alipay_face_refund_amount = $alipay_face_refund_obj->sum('refund_amount'); //退款金额
            $alipay_face_fee_amount = $alipay_face_order_data->sum('fee_amount'); //结算服务费/手续费
            $alipay_face_mdiscount_amount = $alipay_face_order_data->sum('mdiscount_amount'); //商家优惠金额
            $alipay_face_get_amount = $alipay_face_total_amount - $alipay_face_refund_amount - $alipay_face_mdiscount_amount; //商家实收，交易金额-退款金额
            $alipay_face_receipt_amount = $alipay_face_get_amount - $alipay_fee_amount; //实际净额，实收-手续费
            $alipay_face_total_count = '' . count($alipay_face_order_data->get()) . ''; //交易统计
            $alipay_face_refund_count = count($alipay_face_refund_obj->get()); //退款统计

            //微信刷脸
            $weixin_face_total_amount = $weixin_face_order_data->sum('total_amount'); //交易金额
            $weixin_face_refund_amount = $weixin_face_refund_obj->sum('refund_amount'); //退款金额
            $weixin_face_fee_amount = $weixin_face_order_data->sum('fee_amount'); //结算服务费/手续费
            $weixin_face_mdiscount_amount = $weixin_face_order_data->sum('mdiscount_amount'); //商家优惠金额
            $weixin_face_get_amount = $weixin_face_total_amount - $weixin_face_refund_amount - $weixin_face_mdiscount_amount; //商家实收，交易金额-退款金额
            $weixin_face_receipt_amount = $weixin_face_get_amount - $weixin_face_fee_amount; //实际净额，实收-手续费
            $weixin_face_total_count = '' . count($weixin_face_order_data->get()) . '';
            $weixin_face_refund_count = count($weixin_face_refund_obj->get());

            $data = [
                'total_amount' => number_format($total_amount, 2, '.', ''),//交易金额
                'total_count' => '' . $total_count . '',//交易笔数
                'refund_count' => '' . $refund_count . '',//退款笔数
                'discount_count' => '' . $discount_count . '',//优惠笔数
                'discount_amount' => number_format($discount_amount, 2, '.', ''),//优惠金额
                'store_money' => number_format($store_data->store_money, 2, '.', ''),//
                'get_amount' => number_format($get_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                'refund_amount' => number_format($refund_amount, 2, '.', ''),//退款金额
                'receipt_amount' => number_format($receipt_amount, 2, '.', ''),//实际净额，实收-手续费
                'fee_amount' => number_format($fee_amount, 2, '.', ''),//结算服务费/手续费
                'mdiscount_amount' => number_format($mdiscount_amount, 2, '.', ''), //商家优惠金额
                'member_amount' => number_format($total_member_amount, 2, '.', ''), //会员卡支付金额

                'alipay_total_amount' => number_format($alipay_total_amount, 2, '.', ''),//交易金额
                'alipay_total_count' => '' . $alipay_total_count . '',//交易笔数
                'alipay_refund_count' => '' . $alipay_refund_count . '',//退款金额
                'alipay_get_amount' => number_format($alipay_get_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                'alipay_refund_amount' => number_format($alipay_refund_amount, 2, '.', ''),//退款金额
                'alipay_receipt_amount' => number_format($alipay_receipt_amount, 2, '.', ''),//实际净额，实收-手续费
                'alipay_fee_amount' => number_format($alipay_fee_amount, 2, '.', ''),//结算服务费/手续费
                'alipay_mdiscount_amount' => number_format($alipay_mdiscount_amount, 2, '.', ''), //商家优惠

                'weixin_total_amount' => number_format($weixin_total_amount, 2, '.', ''),//交易金额
                'weixin_total_count' => '' . $weixin_total_count . '',//交易笔数
                'weixin_refund_count' => '' . $weixin_refund_count . '',//退款金额
                'weixin_get_amount' => number_format($weixin_get_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                'weixin_refund_amount' => number_format($weixin_refund_amount, 2, '.', ''),//退款金额
                'weixin_receipt_amount' => number_format($weixin_receipt_amount, 2, '.', ''),//实际净额，实收-手续费
                'weixin_fee_amount' => number_format($weixin_fee_amount, 2, '.', ''),//结算服务费/手续费
                'weixin_mdiscount_amount' => number_format($weixin_mdiscount_amount, 2, '.', ''), //商家优惠

                'jd_total_amount' => number_format($jd_total_amount, 2, '.', ''),//交易金额
                'jd_total_count' => '' . $jd_total_count . '',//交易笔数
                'jd_refund_count' => '' . $jd_refund_count . '',//退款金额
                'jd_get_amount' => number_format($jd_get_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                'jd_refund_amount' => number_format($jd_refund_amount, 2, '.', ''),//退款金额
                'jd_receipt_amount' => number_format($jd_receipt_amount, 2, '.', ''),//实际净额，实收-手续费
                'jd_fee_amount' => number_format($jd_fee_amount, 2, '.', ''),//结算服务费/手续费
                'jd_mdiscount_amount' => number_format($jd_mdiscount_amount, 2, '.', ''), //商家优惠

                'un_total_amount' => number_format($un_total_amount, 2, '.', ''),//交易金额
                'un_total_count' => '' . $un_total_count . '',//交易笔数
                'un_refund_count' => '' . $un_refund_count . '',//退款金额
                'un_get_amount' => number_format($un_get_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                'un_refund_amount' => number_format($un_refund_amount, 2, '.', ''),//退款金额
                'un_receipt_amount' => number_format($un_receipt_amount, 2, '.', ''),//实际净额，实收-手续费
                'un_fee_amount' => number_format($un_fee_amount, 2, '.', ''),//结算服务费/手续费
                'un_mdiscount_amount' => number_format($un_mdiscount_amount, 2, '.', ''), //商家优惠

                'unqr_total_amount' => number_format($unqr_total_amount, 2, '.', ''),//交易金额
                'unqr_total_count' => '' . $unqr_total_count . '',//交易笔数
                'unqr_refund_count' => '' . $unqr_refund_count . '',//退款金额
                'unqr_get_amount' => number_format($unqr_get_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                'unqr_refund_amount' => number_format($unqr_refund_amount, 2, '.', ''),//退款金额
                'unqr_receipt_amount' => number_format($unqr_receipt_amount, 2, '.', ''),//实际净额，实收-手续费
                'unqr_fee_amount' => number_format($unqr_fee_amount, 2, '.', ''),//结算服务费/手续费
                'unqr_mdiscount_amount' => number_format($unqr_mdiscount_amount, 2, '.', ''), //商家优惠

                'hbfq_total_amount' => number_format(0, 2, '.', ''),//交易金额
                'hbfq_total_count' => '' . 0 . '',//交易笔数
                'hbfq_refund_count' => '' . 0 . '',//退款金额
                'hbfq_get_amount' => number_format(0, 2, '.', ''),//商家实收，交易金额-退款金额
                'hbfq_refund_amount' => number_format(0, 2, '.', ''),//退款金额
                'hbfq_receipt_amount' => number_format(0, 2, '.', ''),//实际净额，实收-手续费
                'hbfq_fee_amount' => number_format(0, 2, '.', ''),//结算服务费/手续费
                'hbfq_mdiscount_amount' => number_format(0, 2, '.', ''), //商家优惠

                'member_total_amount' => number_format($member_total_amount, 2, '.', ''),//交易金额
                'member_total_count' => '' . $member_total_count . '',//交易笔数
                'member_refund_count' => '' . $member_refund_count . '',//退款金额
                'member_get_amount' => number_format($member_get_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                'member_refund_amount' => number_format($member_refund_amount, 2, '.', ''),//退款金额
                'member_receipt_amount' => number_format($member_receipt_amount, 2, '.', ''),//实际净额，实收-手续费
                'member_fee_amount' => number_format($member_fee_amount, 2, '.', ''),//结算服务费/手续费
                'member_mdiscount_amount' => number_format($member_mdiscount_amount, 2, '.', ''),
                'member_total_deduction' => number_format($member_total_deduction, 2, '.', ''), //会员积分抵扣总值

                'alipay_face_total_amount' => number_format($alipay_face_total_amount, 2, '.', ''),//交易金额
                'alipay_face_total_count' => '' . $alipay_face_total_count . '',//交易笔数
                'alipay_face_refund_count' => '' . $alipay_face_refund_count . '',//退款金额
                'alipay_face_get_amount' => number_format($alipay_face_get_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                'alipay_face_refund_amount' => number_format($alipay_face_refund_amount, 2, '.', ''),//退款金额
                'alipay_face_receipt_amount' => number_format($alipay_face_receipt_amount, 2, '.', ''),//实际净额，实收-手续费
                'alipay_face_fee_amount' => number_format($alipay_face_fee_amount, 2, '.', ''),//结算服务费/手续费
                'alipay_face_mdiscount_amount' => number_format($alipay_face_mdiscount_amount, 2, '.', ''), //商家优惠

                'weixin_face_total_amount' => number_format($weixin_face_total_amount, 2, '.', ''),//交易金额
                'weixin_face_total_count' => '' . $weixin_face_total_count . '',//交易笔数
                'weixin_face_refund_count' => '' . $weixin_face_refund_count . '',//退款金额
                'weixin_face_get_amount' => number_format($weixin_face_get_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                'weixin_face_refund_amount' => number_format($weixin_face_refund_amount, 2, '.', ''),//退款金额
                'weixin_face_receipt_amount' => number_format($weixin_face_receipt_amount, 2, '.', ''),//实际净额，实收-手续费
                'weixin_face_fee_amount' => number_format($weixin_face_fee_amount, 2, '.', ''),//结算服务费/手续费
                'weixin_face_mdiscount_amount' => number_format($weixin_face_mdiscount_amount, 2, '.', ''), //商家优惠

                'total_money' => number_format($store_money->store_money, 2, '.', ''),//剩余余额
                'today_money' => number_format($rata_fee, 2, '.', ''),//今日服务费
                'yesterday_money' => number_format($store_money->yesterday_money, 2, '.', ''), //昨日服务费
            ];

            //打印ID
            $print_data = $data;
            $print_data['store_id'] = $store_id;
            $print_data['merchant_id'] = $merchant_id;
            $print_data['time_start'] = $time_start;
            $print_data['time_end'] = $time_end;

            $data['print_id'] = $store_id . $merchant->merchant_id;
            Cache::put($data['print_id'], json_encode($print_data), 1);

            //附加流水详情
            if ($return_type == "02") {
                if ($ways_source) {
                    if (in_array($ways_source, ['alipay_face', 'weixin_face'])) {
                        $where[] = ['pay_method', '=', $ways_source];
                    } else {
                        $where[] = ['ways_source', '=', $ways_source];
                    }
                }

                if (env('DB_D1_HOST')) {
                    //有没有跨天
                    if ($is_ct_time) {
                        if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                            $obj6 = DB::connection("mysql_d1")->table('order_items');
                        } else {
                            $obj6 = DB::connection("mysql_d1")->table('orders');
                        }
                    } else {
                        if (Schema::hasTable($table)) {
                            $obj6 = DB::connection("mysql_d1")->table($table);
                        } else {
                            if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                                $obj6 = DB::connection("mysql_d1")->table('order_items');
                            } else {
                                $obj6 = DB::connection("mysql_d1")->table('orders');
                            }
                        }
                    }
                } else {
                    if ($is_ct_time) {
                        if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                            $obj6 = DB::table('order_items');
                        } else {
                            $obj6 = DB::table('orders');
                        }
                    } else {
                        if (Schema::hasTable($table)) {
                            $obj6 = DB::table($table);
                        } else {
                            if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                                $obj6 = DB::table('order_items');
                            } else {
                                $obj6 = DB::table('orders');
                            }
                        }
                    }
                }

                if ($pay_status) {
                    $where[] = ['pay_status', '=', $pay_status];
                }

                $obj6 = $obj6->where($where)
                    ->whereIn('store_id', $store_ids)
                    ->orderBy('created_at', 'desc');
                $this->t = $obj6->count();
                $data['order_list'] = $this->page($obj6)->get();
            }

            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }
    
    // 获取俩时间间隔日期
    public function getDateFromRanges($startdate, $enddate){
        $stimestamp = strtotime($startdate);
        $etimestamp = strtotime($enddate);

        $days = ($etimestamp - $stimestamp) / 86400; // 计算日期段内有多少天
//        print_r( $days );die;
        $date = array(); // 保存每天日期

        for($i = 0; $i <= $days; $i++){
            $date[] = date( 'Y-m-d', $stimestamp + (86400 * $i) );
        }
        return $date;
    }
    
    //获取俩时间间隔日期
    public function getDateFromRange($startdate, $enddate){

        $stimestamp = strtotime($startdate);
        $etimestamp = strtotime($enddate);

        // 计算日期段内有多少天
        $days = ($etimestamp-$stimestamp)/86400+1;

        // 保存每天日期
        $date = array();

        for($i=0; $i<$days; $i++){
            $date[] = date('Y-m-d', $stimestamp+(86400*$i));
        }

        return $date;
    }

    //账单统计
    public function order_bill(Request $request){
        $merchant = $this->parseToken();
        $curHdIndex = $request->get('curHdIndex', '');
        $curtypeIndex = $request->get('curtypeIndex', '');
        $store_id=$request->get('store_id', '');
        $MerchantStore = MerchantStore::where('merchant_id', $merchant->merchant_id)->select('store_id')->get();
        $store_ids = $MerchantStore->toArray();
        // print_r($store_ids);die;
        if($curHdIndex==1){
            $where[] = ['created_at', '>=', date('Y-m-d').' 00:00:00'];
            $where[] = ['created_at', '<=', date('Y-m-d').' 23:59:59'];
            //总收款
            $total_amount=Order::whereIn('store_id', $store_ids)->where($where)->whereIn('pay_status', [1, 6])->select('total_amount')->sum('total_amount');
            //总手续费
            $fee_amount=Order::whereIn('store_id', $store_ids)->where($where)->whereIn('pay_status', [1, 6])->select('fee_amount')->sum('fee_amount');

            //总退款
            $refund_amount=Order::whereIn('store_id', $store_ids)->where($where)->whereIn('pay_status', [6])->select('refund_amount')->sum('refund_amount');
            //总收款笔数
            $total_num=count(Order::whereIn('store_id', $store_ids)->where($where)->whereIn('pay_status', [1, 6])->select('total_amount')->get());

            //总退款笔数
            $refund_num=count(Order::whereIn('store_id', $store_ids)->where($where)->whereIn('pay_status', [6])->select('refund_amount')->get());
            $final_amount=bcsub($total_amount,$fee_amount+$refund_amount,2);
            if($curtypeIndex==1){
                $merchant_stores = DB::table('merchant_stores')->whereIn('store_id', $store_ids)->select()->get();
                $merchant_stores=json_decode($merchant_stores,true);

                foreach($merchant_stores as $key=>$val){
                    $merchant=DB::table('merchants')->where(array('id'=>$val['merchant_id']))->first();
                    $merchant_amount = Order::where('store_id', $val['store_id'])->where($where)->whereIn('pay_status', [1, 6])->where('merchant_id', $val['merchant_id'])->select('total_amount')->sum('total_amount');
                    $merchant_num = count(Order::where('store_id', $val['store_id'])->where($where)->whereIn('pay_status', [1, 6])->where('merchant_id', $val['merchant_id'])->select('total_amount')->get());
                    $store=DB::table('stores')->where(array('store_id'=>$val['store_id']))->first();
                    if($merchant_amount>0){
                        if($store->pid==0){
                            $merchant_item[$key]['store_name']='总店：'.$store->store_name;
                            $merchant_item[$key]['merchant_name']=$merchant->name;
                            $merchant_item[$key]['merchant_num']='交易笔数：'.$merchant_num.'笔';
                            $merchant_item[$key]['money_name']='入账金额';
                            $merchant_item[$key]['merchant_id']=$val['merchant_id'];
                            $merchant_item[$key]['merchant_amount']=$merchant_amount;
                        }else{
                            $merchant_item[$key]['store_name']='分店：'.$store->store_name;
                            $merchant_item[$key]['merchant_name']=$merchant->name;
                            $merchant_item[$key]['merchant_num']='交易笔数：'.$merchant_num.'笔';
                            $merchant_item[$key]['money_name']='入账金额';
                            $merchant_item[$key]['merchant_id']=$val['merchant_id'];
                            $merchant_item[$key]['merchant_amount']=$merchant_amount;
                        }
                    }else{}
                }
                if(empty($merchant_item)){
                    $merchant_item=array();
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$total_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$total_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
            if($curtypeIndex==2){
                $ways_source=array(0=>'weixin',1=>'alipay',3=>'unionpay',4=>'Bestpay');
                foreach($ways_source as $k=>$v){
                    if($v=='weixin'){
                        $way_type='微信支付';
                    }else if($v=='alipay'){
                        $way_type='支付宝支付';
                    }else if($v=='unionpay'){
                        $way_type='云闪付支付';
                    }else if($v=='Bestpay'){
                        $way_type='翼支付支付';
                    }
                    $ways_amount=Order::whereIn('store_id', $store_ids)->where($where)->where('ways_source', $v)->whereIn('pay_status', [1, 6])->select('total_amount')->sum('total_amount');
                    $ways_sum=count(Order::whereIn('store_id', $store_ids)->where($where)->where('ways_source', $v)->whereIn('pay_status', [1, 6])->select('total_amount')->get());
                    $merchant_item[$k]['store_name']='交易类型';
                    $merchant_item[$k]['merchant_name']=$way_type;
                    $merchant_item[$k]['merchant_num']='交易笔数：'.$ways_sum.'笔';
                    $merchant_item[$k]['money_name']='入账金额';
                    $merchant_item[$k]['merchant_amount']=$ways_amount;
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$total_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$total_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
            if($curtypeIndex==3){
                $merchant_item[0]['store_name']='交易日期';
                $merchant_item[0]['merchant_name']=date('Y-m-d');
                $merchant_item[0]['merchant_num']='交易笔数：'.$total_num.'笔';
                $merchant_item[0]['money_name']='入账金额';
                $merchant_item[0]['merchant_amount']=$total_amount;
                $return=array('total_num'=>$total_num,'total_amount'=>$total_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$total_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
        }
        if($curHdIndex==2){
            $where[] = ['settling_day', '>=', date('Y-m-d',strtotime("-1 day"))];
            $where[] = ['settling_day', '<=', date('Y-m-d',strtotime("-1 day"))];
            $store_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_amount')->sum('store_amount');

            $total_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_order_count')->sum('store_order_count');

            $refund_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_amount')->sum('refund_amount');

            $refund_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_order_num')->sum('refund_order_num');

            $fee_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('fee_amount')->sum('fee_amount');
            $final_amount=bcsub($store_amount,$fee_amount+$refund_amount,2);
            if($curtypeIndex==1){
                $merchant_stores=DB::table('merchant_stores')->whereIn('store_id', $store_ids)->select('*')->get();
                $merchant_stores=json_decode($merchant_stores,true);

                foreach($merchant_stores as $key=>$val){
                    $merchant=DB::table('merchants')->where(array('id'=>$val['merchant_id']))->first();
                    $store=DB::table('stores')->where(array('store_id'=>$val['store_id']))->first();
                    $merchant_order_num=DB::table('stores_cash_count')->where('store_id', $val['store_id'])->where('merchant_id', $val['merchant_id'])->where($where)->select('merchant_num')->sum('merchant_num');
                    $merchant_order_money=DB::table('stores_cash_count')->where('store_id', $val['store_id'])->where('merchant_id', $val['merchant_id'])->where($where)->select('merchant_money')->sum('merchant_money');
                    if($merchant_order_money>0){
                        if($store->pid==0){
                            $merchant_item[$key]['store_name']='总店：'.$store->store_name;
                            $merchant_item[$key]['merchant_name']=$merchant->name;
                            $merchant_item[$key]['merchant_num']='交易笔数：'.$merchant_order_num.'笔';
                            $merchant_item[$key]['money_name']='入账金额';
                            $merchant_item[$key]['merchant_id']=$val['merchant_id'];
                            $merchant_item[$key]['merchant_amount']=$merchant_order_money;
                        }else{
                            $merchant_item[$key]['store_name']='分店：'.$store->store_name;
                            $merchant_item[$key]['merchant_name']=$merchant->name;
                            $merchant_item[$key]['merchant_num']='交易笔数：'.$merchant_order_num.'笔';
                            $merchant_item[$key]['money_name']='入账金额';
                            $merchant_item[$key]['merchant_id']=$val['merchant_id'];
                            $merchant_item[$key]['merchant_amount']=$merchant_order_money;
                        }
                    }else{

                    }
                }
                if(empty($merchant_item)){
                    $merchant_item=array();
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$store_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$store_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
            if($curtypeIndex==2){
                $ways_source=array(0=>'weixin',1=>'alipay',3=>'unionpay',4=>'Bestpay');
                foreach($ways_source as $k=>$v){
                    if($v=='weixin'){
                        $way_type='微信支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('weixin_amount')->sum('weixin_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('weixin_num')->sum('weixin_num');
                    }else if($v=='alipay'){
                        $way_type='支付宝支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('alipay_amount')->sum('alipay_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('alipay_num')->sum('alipay_num');
                    }else if($v=='unionpay'){
                        $way_type='云闪付支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('unionpay_amount')->sum('unionpay_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('unionpay_num')->sum('unionpay_num');
                    }else if($v=='Bestpay'){
                        $way_type='翼支付支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('Bestpay_amount')->sum('Bestpay_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('Bestpay_num')->sum('Bestpay_num');
                    }
                    $merchant_item[$k]['store_name']='交易类型';
                    $merchant_item[$k]['merchant_name']=$way_type;
                    $merchant_item[$k]['merchant_num']='交易笔数：'.$ways_sum.'笔';
                    $merchant_item[$k]['money_name']='入账金额';
                    $merchant_item[$k]['merchant_amount']=$ways_amount;
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$store_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$store_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
            if($curtypeIndex==3){
                $date=$this->getDateFromRange(date('Y-m-d',strtotime("-1 day")),date('Y-m-d',strtotime("-1 day")));
                foreach($date as $k=>$v){
                    $data_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where('settling_day',$v)->select('store_amount')->sum('store_amount');
                    $data_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where('settling_day',$v)->select('store_order_count')->sum('store_order_count');
                    $merchant_item[$k]['store_name']='交易日期';
                    $merchant_item[$k]['merchant_name']=$v;
                    $merchant_item[$k]['merchant_num']='交易笔数：'.$data_num.'笔';
                    $merchant_item[$k]['money_name']='入账金额';
                    $merchant_item[$k]['merchant_amount']=$data_amount;
                    $return=array('total_num'=>$total_num,'total_amount'=>$store_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$store_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                    $this->status = 1;
                    $this->message = '数据返回成功';
                    return $this->format($return);
                }
            }
        }
        if($curHdIndex==3){
            $where[] = ['settling_day', '>=', date('Y-m-d', strtotime("this week"))];
            $where[] = ['settling_day', '<=', date('Y-m-d',strtotime("-1 day"))];
            $store_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_amount')->sum('store_amount');

            $total_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_order_count')->sum('store_order_count');

            $refund_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_amount')->sum('refund_amount');

            $refund_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_order_num')->sum('refund_order_num');

            $fee_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('fee_amount')->sum('fee_amount');

            $where1[] = ['created_at', '>=', date('Y-m-d').' 00:00:00'];
            $where1[] = ['created_at', '<=', date('Y-m-d').' 23:59:59'];
            //今日总收款
            // $total_amount1=Order::whereIn('store_id', $store_ids)->where($where1)->whereIn('pay_status', [1, 6])->select('total_amount')->sum('total_amount');
            $total_amount1=0;
            //今日总手续费
            // $fee_amount1=Order::whereIn('store_id', $store_ids)->where($where1)->whereIn('pay_status', [1, 6])->select('fee_amount')->sum('fee_amount');
            $fee_amount1=0;
            //今日总退款
            // $refund_amount1=Order::whereIn('store_id', $store_ids)->where($where1)->whereIn('pay_status', [6])->select('refund_amount')->sum('refund_amount');
            $refund_amount1=0;
            //今日总收款笔数
            // $total_num1=count(Order::whereIn('store_id', $store_ids)->where($where1)->whereIn('pay_status', [1, 6])->select('total_amount')->get());
            $total_num1=0;
            //今日总退款笔数
            // $refund_num1=count(Order::whereIn('store_id', $store_ids)->where($where1)->whereIn('pay_status', [6])->select('refund_amount')->get());
            $refund_num1=0;

            $store_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_amount')->sum('store_amount');
            $store_amount=bcadd($store_amount,$total_amount1,2);

            $total_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_order_count')->sum('store_order_count');
            $total_num=$total_num+$total_num1;

            $refund_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_amount')->sum('refund_amount');
            $refund_amount=$refund_amount+$refund_amount1;

            $refund_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_order_num')->sum('refund_order_num');
            $refund_num=$refund_num+$refund_num1;

            $fee_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('fee_amount')->sum('fee_amount');
            $fee_amount=bcadd($fee_amount,$fee_amount1,2);
            $final_amount=bcsub($store_amount,$fee_amount+$refund_amount,2);
            if($curtypeIndex==1){
                $merchant_stores=DB::table('merchant_stores')->whereIn('store_id', $store_ids)->select('*')->get();
                $merchant_stores=json_decode($merchant_stores,true);

                foreach($merchant_stores as $key=>$val){
                    $merchant=DB::table('merchants')->where(array('id'=>$val['merchant_id']))->first();
                    $store=DB::table('stores')->where(array('store_id'=>$val['store_id']))->first();
                    $merchant_order_num=DB::table('stores_cash_count')->where('store_id', $val['store_id'])->where('merchant_id', $val['merchant_id'])->where($where)->select('merchant_num')->sum('merchant_num');
                    $merchant_order_money=DB::table('stores_cash_count')->where('store_id', $val['store_id'])->where('merchant_id', $val['merchant_id'])->where($where)->select('merchant_money')->sum('merchant_money');
                    // $merchant_amount = Order::where('store_id', $val['store_id'])->where($where1)->whereIn('pay_status', [1, 6])->where('merchant_id', $val['merchant_id'])->select('total_amount')->sum('total_amount');
                    $merchant_amount=0;
                    // $merchant_num = count(Order::where('store_id', $val['store_id'])->where($where1)->whereIn('pay_status', [1, 6])->where('merchant_id', $val['merchant_id'])->select('total_amount')->get());
                    $merchant_num=0;
                    $merchant_order_num=$merchant_order_num+$merchant_num;
                    $merchant_order_money=$merchant_order_money+$merchant_num;
                    if($merchant_order_money>0){
                        if($store->pid==0){
                            $merchant_item[$key]['store_name']='总店：'.$store->store_name;
                            $merchant_item[$key]['merchant_name']=$merchant->name;
                            $merchant_item[$key]['merchant_num']='交易笔数：'.$merchant_order_num.'笔';
                            $merchant_item[$key]['money_name']='入账金额';
                            $merchant_item[$key]['merchant_amount']=$merchant_order_money;
                            $merchant_item[$key]['merchant_id']=$val['merchant_id'];
                        }else{
                            $merchant_item[$key]['store_name']='分店：'.$store->store_name;
                            $merchant_item[$key]['merchant_name']=$merchant->name;
                            $merchant_item[$key]['merchant_num']='交易笔数：'.$merchant_order_num.'笔';
                            $merchant_item[$key]['money_name']='入账金额';
                            $merchant_item[$key]['merchant_amount']=$merchant_order_money;
                            $merchant_item[$key]['merchant_id']=$val['merchant_id'];
                        }
                    }else{

                    }
                }
                if(empty($merchant_item)){
                    $merchant_item=array();
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$store_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$store_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
            if($curtypeIndex==2){
                $ways_source=array(0=>'weixin',1=>'alipay',3=>'unionpay',4=>'Bestpay');
                foreach($ways_source as $k=>$v){
                    if($v=='weixin'){
                        $way_type='微信支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('weixin_amount')->sum('weixin_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('weixin_num')->sum('weixin_num');
                    }else if($v=='alipay'){
                        $way_type='支付宝支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('alipay_amount')->sum('alipay_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('alipay_num')->sum('alipay_num');
                    }else if($v=='unionpay'){
                        $way_type='云闪付支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('unionpay_amount')->sum('unionpay_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('unionpay_num')->sum('unionpay_num');
                    }else if($v=='Bestpay'){
                        $way_type='翼支付支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('Bestpay_amount')->sum('Bestpay_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('Bestpay_num')->sum('Bestpay_num');
                    }
                    $merchant_item[$k]['store_name']='交易类型';
                    $merchant_item[$k]['merchant_name']=$way_type;
                    $merchant_item[$k]['merchant_num']='交易笔数：'.$ways_sum.'笔';
                    $merchant_item[$k]['money_name']='入账金额';
                    $merchant_item[$k]['merchant_amount']=$ways_amount;
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$store_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$store_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
            if($curtypeIndex==3){
                $date=$this->getDateFromRange(date('Y-m-d', strtotime("this week")),date('Y-m-d',strtotime("-1 day")));
                foreach($date as $k=>$v){
                    $data_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where('settling_day',$v)->select('store_amount')->sum('store_amount');
                    $data_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where('settling_day',$v)->select('store_order_count')->sum('store_order_count');
                    $merchant_item[$k]['store_name']='交易日期';
                    $merchant_item[$k]['merchant_name']=$v;
                    $merchant_item[$k]['merchant_num']='交易笔数：'.$data_num.'笔';
                    $merchant_item[$k]['money_name']='入账金额';
                    $merchant_item[$k]['merchant_amount']=$data_amount;
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$store_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>bcsub($store_amount,$refund_amount+$refund_amount,2),'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
        }
        if($curHdIndex==4){
            $where[] = ['settling_day', '>=', date('Y-m-d', strtotime('last week'))];
            $where[] = ['settling_day', '<=', date('Y-m-d', strtotime('this week -1 day'))];
            $store_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_amount')->sum('store_amount');

            $total_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_order_count')->sum('store_order_count');

            $refund_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_amount')->sum('refund_amount');

            $refund_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_order_num')->sum('refund_order_num');

            $fee_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('fee_amount')->sum('fee_amount');
            $final_amount=bcsub($store_amount,$fee_amount+$refund_amount,2);
            if($curtypeIndex==1){
                $merchant_stores=DB::table('merchant_stores')->whereIn('store_id', $store_ids)->select('*')->get();
                $merchant_stores=json_decode($merchant_stores,true);

                foreach($merchant_stores as $key=>$val){
                    $merchant=DB::table('merchants')->where(array('id'=>$val['merchant_id']))->first();
                    $store=DB::table('stores')->where(array('store_id'=>$val['store_id']))->first();
                    $merchant_order_num=DB::table('stores_cash_count')->where('store_id', $val['store_id'])->where('merchant_id', $val['merchant_id'])->where($where)->select('merchant_num')->sum('merchant_num');
                    $merchant_order_money=DB::table('stores_cash_count')->where('store_id', $val['store_id'])->where('merchant_id', $val['merchant_id'])->where($where)->select('merchant_money')->sum('merchant_money');
                    if($merchant_order_money>0){
                        if($store->pid==0){
                            $merchant_item[$key]['store_name']='总店：'.$store->store_name;
                            $merchant_item[$key]['merchant_name']=$merchant->name;
                            $merchant_item[$key]['merchant_num']='交易笔数：'.$merchant_order_num.'笔';
                            $merchant_item[$key]['money_name']='入账金额';
                            $merchant_item[$key]['merchant_amount']=$merchant_order_money;
                            $merchant_item[$key]['merchant_id']=$val['merchant_id'];
                        }else{
                            $merchant_item[$key]['store_name']='分店：'.$store->store_name;
                            $merchant_item[$key]['merchant_name']=$merchant->name;
                            $merchant_item[$key]['merchant_num']='交易笔数：'.$merchant_order_num.'笔';
                            $merchant_item[$key]['money_name']='入账金额';
                            $merchant_item[$key]['merchant_amount']=$merchant_order_money;
                            $merchant_item[$key]['merchant_id']=$val['merchant_id'];
                        }
                    }else{

                    }
                }
                if(empty($merchant_item)){
                    $merchant_item=array();
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$store_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$store_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
            if($curtypeIndex==2){
                $ways_source=array(0=>'weixin',1=>'alipay',3=>'unionpay',4=>'Bestpay');
                foreach($ways_source as $k=>$v){
                    if($v=='weixin'){
                        $way_type='微信支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('weixin_amount')->sum('weixin_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('weixin_num')->sum('weixin_num');
                    }else if($v=='alipay'){
                        $way_type='支付宝支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('alipay_amount')->sum('alipay_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('alipay_num')->sum('alipay_num');
                    }else if($v=='unionpay'){
                        $way_type='云闪付支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('unionpay_amount')->sum('unionpay_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('unionpay_num')->sum('unionpay_num');
                    }else if($v=='Bestpay'){
                        $way_type='翼支付支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('Bestpay_amount')->sum('Bestpay_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('Bestpay_num')->sum('Bestpay_num');
                    }
                    $merchant_item[$k]['store_name']='交易类型';
                    $merchant_item[$k]['merchant_name']=$way_type;
                    $merchant_item[$k]['merchant_num']='交易笔数：'.$ways_sum.'笔';
                    $merchant_item[$k]['money_name']='入账金额';
                    $merchant_item[$k]['merchant_amount']=$ways_amount;
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$store_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$store_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
            if($curtypeIndex==3){
                $date=$this->getDateFromRange(date('Y-m-d', strtotime('last week')),date('Y-m-d', strtotime('this week -1 day')));
                foreach($date as $k=>$v){
                    $data_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where('settling_day',$v)->select('store_amount')->sum('store_amount');
                    $data_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where('settling_day',$v)->select('store_order_count')->sum('store_order_count');
                    $merchant_item[$k]['store_name']='交易日期';
                    $merchant_item[$k]['merchant_name']=$v;
                    $merchant_item[$k]['merchant_num']='交易笔数：'.$data_num.'笔';
                    $merchant_item[$k]['money_name']='入账金额';
                    $merchant_item[$k]['merchant_amount']=$data_amount;
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$store_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$store_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
        }
        if($curHdIndex==5){
            $where[] = ['settling_day', '>=', date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00'))];
            $where[] = ['settling_day', '<=', date('Y-m-d',strtotime("-1 day"))];
            $store_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_amount')->sum('store_amount');

            $total_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_order_count')->sum('store_order_count');

            $refund_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_amount')->sum('refund_amount');

            $refund_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_order_num')->sum('refund_order_num');

            $fee_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('fee_amount')->sum('fee_amount');

            $where1[] = ['created_at', '>=', date('Y-m-d').' 00:00:00'];
            $where1[] = ['created_at', '<=', date('Y-m-d').' 23:59:59'];
            //今日总收款
            // $total_amount1=Order::whereIn('store_id', $store_ids)->where($where1)->whereIn('pay_status', [1, 6])->select('total_amount')->sum('total_amount');
            $total_amount1=0;
            //今日总手续费
            // $fee_amount1=Order::whereIn('store_id', $store_ids)->where($where1)->whereIn('pay_status', [1, 6])->select('fee_amount')->sum('fee_amount');
            $fee_amount1=0;
            //今日总退款
            // $refund_amount1=Order::whereIn('store_id', $store_ids)->where($where1)->whereIn('pay_status', [6])->select('refund_amount')->sum('refund_amount');
            $refund_amount1=0;
            //今日总收款笔数
            // $total_num1=count(Order::whereIn('store_id', $store_ids)->where($where1)->whereIn('pay_status', [1, 6])->select('total_amount')->get());
            $total_num1=0;
            //今日总退款笔数
            // $refund_num1=count(Order::whereIn('store_id', $store_ids)->where($where1)->whereIn('pay_status', [6])->select('refund_amount')->get());
            $refund_num1=0;

            $store_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_amount')->sum('store_amount');
            $store_amount=$store_amount+$total_amount1;
            $store_amount = sprintf( "%.2f", $store_amount );
            // print_r($store_amount);die;

            $total_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_order_count')->sum('store_order_count');
            // $total_num=$total_num+$total_num1;
            $total_num=bcadd($total_num,$total_num1,2);

            $refund_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_amount')->sum('refund_amount');
            $refund_amount=$refund_amount+$refund_amount1;

            $refund_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_order_num')->sum('refund_order_num');
            $refund_num=$refund_num+$refund_num1;

            $fee_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('fee_amount')->sum('fee_amount');
            $fee_amount=$fee_amount+$fee_amount1;
            $final_amount=bcsub($store_amount,$fee_amount+$refund_amount,2);
            // print_r($curtypeIndex);die;
            if($curtypeIndex==1){
                $merchant_stores=DB::table('merchant_stores')->whereIn('store_id', $store_ids)->select('*')->get();
                $merchant_stores=json_decode($merchant_stores,true);

                foreach($merchant_stores as $key=>$val){
                    $merchant=DB::table('merchants')->where(array('id'=>$val['merchant_id']))->first();
                    $store=DB::table('stores')->where(array('store_id'=>$val['store_id']))->first();
                    $merchant_order_num=DB::table('stores_cash_count')->where('store_id', $val['store_id'])->where('merchant_id', $val['merchant_id'])->where($where)->select('merchant_num')->sum('merchant_num');
                    $merchant_order_money=DB::table('stores_cash_count')->where('store_id', $val['store_id'])->where('merchant_id', $val['merchant_id'])->where($where)->select('merchant_money')->sum('merchant_money');
                    // $merchant_amount = Order::where('store_id', $val['store_id'])->where($where1)->whereIn('pay_status', [1, 6])->where('merchant_id', $val['merchant_id'])->select('total_amount')->sum('total_amount');
                    $merchant_amount=0;
                    // $merchant_num = count(Order::where('store_id', $val['store_id'])->where($where1)->whereIn('pay_status', [1, 6])->where('merchant_id', $val['merchant_id'])->select('total_amount')->get());
                    $merchant_num=0;
                    $merchant_order_num=$merchant_order_num+$merchant_num;
                    $merchant_order_money=$merchant_order_money+$merchant_num;

                    if($merchant_order_money>0){
                        if($store->pid==0){
                            $merchant_item[$key]['store_name']='总店：'.$store->store_name;
                            $merchant_item[$key]['merchant_name']=$merchant->name;
                            $merchant_item[$key]['merchant_num']='交易笔数：'.$merchant_order_num.'笔';
                            $merchant_item[$key]['money_name']='入账金额';
                            $merchant_item[$key]['merchant_amount']=$merchant_order_money;
                            $merchant_item[$key]['merchant_id']=$val['merchant_id'];
                        }else{
                            $merchant_item[$key]['store_name']='分店：'.$store->store_name;
                            $merchant_item[$key]['merchant_name']=$merchant->name;
                            $merchant_item[$key]['merchant_num']='交易笔数：'.$merchant_order_num.'笔';
                            $merchant_item[$key]['money_name']='入账金额';
                            $merchant_item[$key]['merchant_amount']=$merchant_order_money;
                            $merchant_item[$key]['merchant_id']=$val['merchant_id'];
                        }
                    }else{

                    }
                }
                if(empty($merchant_item)){
                    $merchant_item=array();
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$store_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$store_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
            if($curtypeIndex==2){
                $ways_source=array(0=>'weixin',1=>'alipay',3=>'unionpay',4=>'Bestpay');
                foreach($ways_source as $k=>$v){
                    if($v=='weixin'){
                        $way_type='微信支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('weixin_amount')->sum('weixin_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('weixin_num')->sum('weixin_num');
                    }else if($v=='alipay'){
                        $way_type='支付宝支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('alipay_amount')->sum('alipay_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('alipay_num')->sum('alipay_num');
                    }else if($v=='unionpay'){
                        $way_type='云闪付支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('unionpay_amount')->sum('unionpay_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('unionpay_num')->sum('unionpay_num');
                    }else if($v=='Bestpay'){
                        $way_type='翼支付支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('Bestpay_amount')->sum('Bestpay_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('Bestpay_num')->sum('Bestpay_num');
                    }
                    $merchant_item[$k]['store_name']='交易类型';
                    $merchant_item[$k]['merchant_name']=$way_type;
                    $merchant_item[$k]['merchant_num']='交易笔数：'.$ways_sum.'笔';
                    $merchant_item[$k]['money_name']='入账金额';
                    $merchant_item[$k]['merchant_amount']=$ways_amount;
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$store_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$store_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
            if($curtypeIndex==3){
                $date=$this->getDateFromRange(date('Y-m-d', strtotime(date('Y-m', time()).'-01 00:00:00')),date('Y-m-d',strtotime("-1 day")));
                foreach($date as $k=>$v){
                    $data_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where('settling_day',$v)->select('store_amount')->sum('store_amount');
                    $data_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where('settling_day',$v)->select('store_order_count')->sum('store_order_count');
                    $merchant_item[$k]['store_name']='交易日期';
                    $merchant_item[$k]['merchant_name']=$v;
                    $merchant_item[$k]['merchant_num']='交易笔数：'.$data_num.'笔';
                    $merchant_item[$k]['money_name']='入账金额';
                    $merchant_item[$k]['merchant_amount']=$data_amount;
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$store_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$store_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
        }
        if($curHdIndex==6){
            $where[] = ['settling_day', '>=', date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00')))];
            $where[] = ['settling_day', '<=', date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00') - 86400)];
            $store_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_amount')->sum('store_amount');

            $total_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_order_count')->sum('store_order_count');

            $refund_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_amount')->sum('refund_amount');

            $refund_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_order_num')->sum('refund_order_num');

            $fee_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('fee_amount')->sum('fee_amount');
            $final_amount=bcsub($store_amount,$fee_amount+$refund_amount,2);
            if($curtypeIndex==1){
                $merchant_stores=DB::table('merchant_stores')->whereIn('store_id', $store_ids)->select('*')->get();
                $merchant_stores=json_decode($merchant_stores,true);

                foreach($merchant_stores as $key=>$val){
                    $merchant=DB::table('merchants')->where(array('id'=>$val['merchant_id']))->first();
                    $store=DB::table('stores')->where(array('store_id'=>$val['store_id']))->first();
                    $merchant_order_num=DB::table('stores_cash_count')->where('store_id', $val['store_id'])->where('merchant_id', $val['merchant_id'])->where($where)->select('merchant_num')->sum('merchant_num');
                    $merchant_order_money=DB::table('stores_cash_count')->where('store_id', $val['store_id'])->where('merchant_id', $val['merchant_id'])->where($where)->select('merchant_money')->sum('merchant_money');
                    if($merchant_order_money>0){
                        if($store->pid==0){
                            $merchant_item[$key]['store_name']='总店：'.$store->store_name;
                            $merchant_item[$key]['merchant_name']=$merchant->name;
                            $merchant_item[$key]['merchant_num']='交易笔数：'.$merchant_order_num.'笔';
                            $merchant_item[$key]['money_name']='入账金额';
                            $merchant_item[$key]['merchant_amount']=$merchant_order_money;
                            $merchant_item[$key]['merchant_id']=$val['merchant_id'];
                        }else{
                            $merchant_item[$key]['store_name']='分店：'.$store->store_name;
                            $merchant_item[$key]['merchant_name']=$merchant->name;
                            $merchant_item[$key]['merchant_num']='交易笔数：'.$merchant_order_num.'笔';
                            $merchant_item[$key]['money_name']='入账金额';
                            $merchant_item[$key]['merchant_amount']=$merchant_order_money;
                            $merchant_item[$key]['merchant_id']=$val['merchant_id'];
                        }
                    }else{

                    }
                }
                if(empty($merchant_item)){
                    $merchant_item=array();
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$store_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$store_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
            if($curtypeIndex==2){
                $ways_source=array(0=>'weixin',1=>'alipay',3=>'unionpay',4=>'Bestpay');
                foreach($ways_source as $k=>$v){
                    if($v=='weixin'){
                        $way_type='微信支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('weixin_amount')->sum('weixin_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('weixin_num')->sum('weixin_num');
                    }else if($v=='alipay'){
                        $way_type='支付宝支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('alipay_amount')->sum('alipay_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('alipay_num')->sum('alipay_num');
                    }else if($v=='unionpay'){
                        $way_type='云闪付支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('unionpay_amount')->sum('unionpay_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('unionpay_num')->sum('unionpay_num');
                    }else if($v=='Bestpay'){
                        $way_type='翼支付支付';
                        $ways_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('Bestpay_amount')->sum('Bestpay_amount');;
                        $ways_sum=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('Bestpay_num')->sum('Bestpay_num');
                    }
                    $merchant_item[$k]['store_name']='交易类型';
                    $merchant_item[$k]['merchant_name']=$way_type;
                    $merchant_item[$k]['merchant_num']='交易笔数：'.$ways_sum.'笔';
                    $merchant_item[$k]['money_name']='入账金额';
                    $merchant_item[$k]['merchant_amount']=$ways_amount;
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$store_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$store_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
            if($curtypeIndex==3){
                $date=$this->getDateFromRange(date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00'))),date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00') - 86400));
                foreach($date as $k=>$v){
                    $data_amount=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where('settling_day',$v)->select('store_amount')->sum('store_amount');
                    $data_num=DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where('settling_day',$v)->select('store_order_count')->sum('store_order_count');
                    $merchant_item[$k]['store_name']='交易日期';
                    $merchant_item[$k]['merchant_name']=$v;
                    $merchant_item[$k]['merchant_num']='交易笔数：'.$data_num.'笔';
                    $merchant_item[$k]['money_name']='入账金额';
                    $merchant_item[$k]['merchant_amount']=$data_amount;
                }
                $return=array('total_num'=>$total_num,'total_amount'=>$store_amount,'refund_num'=>$refund_num,'refund_amount'=>$refund_amount,'real_amount'=>$store_amount-$refund_amount,'fee_amount'=>$fee_amount,'final_amount'=>$final_amount,'merchant_list'=>$merchant_item);
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($return);
            }
        }
    }

    //服务费汇总
    public function server_day(Request $request){
        $merchant = $this->parseToken();
        $curHdIndex = $request->get('curHdIndex', '');
        $MerchantStore = MerchantStore::where('merchant_id', $merchant->merchant_id)->select('store_id')->get();
        $store_ids = $MerchantStore->toArray();
        $head_store=DB::table('stores')->where('store_id', $store_ids[0]['store_id'])->first();
        if($curHdIndex==1){
            $where[] = ['settling_day', '>=', date('Y-m-d',strtotime("-1 day"))];
            $where[] = ['settling_day', '<=', date('Y-m-d',strtotime("-1 day"))];
            $service_day=DB::table('service_day_money')->whereIn('store_id', $store_ids)->where($where)->get();
            $service_day_count=DB::table('service_day_money')->whereIn('store_id', $store_ids)->where($where)->select('count')->sum('count');
            $return=array('service_day_count'=>$service_day_count,'service_day'=>$service_day,'store_name'=>$head_store->store_name);
            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($return);
        }else if ($curHdIndex==2){
            $date=$this->getDateFromRange(date('Y-m-d', strtotime("this week")),date('Y-m-d',strtotime("-1 day")));
            $service_day=DB::table('service_day_money')->whereIn('store_id', $store_ids)->whereIn('settling_day',$date)->orderBy('settling_day','desc')->get();
            $service_day_count=DB::table('service_day_money')->whereIn('store_id', $store_ids)->whereIn('settling_day',$date)->select('count')->sum('count');
            $return=array('service_day_count'=>$service_day_count,'service_day'=>$service_day,'store_name'=>$head_store->store_name);
            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($return);
        }else if($curHdIndex==3){
            $date=$this->getDateFromRange(date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00')),date('Y-m-d',strtotime("-1 day")));
            $service_day=DB::table('service_day_money')->whereIn('store_id', $store_ids)->whereIn('settling_day',$date)->orderBy('settling_day','desc')->get();
            $service_day_count=DB::table('service_day_money')->whereIn('store_id', $store_ids)->whereIn('settling_day',$date)->select('count')->sum('count');
            $return=array('service_day_count'=>$service_day_count,'service_day'=>$service_day,'store_name'=>$head_store->store_name);
            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($return);
        }
    }

    //服务费查询
    public function server_search(Request $request){
        $merchant = $this->parseToken();
        $starttime = $request->get('starttime', '');
        $endtime = $request->get('endtime', '');
        $MerchantStore = MerchantStore::where('merchant_id', $merchant->merchant_id)->select('store_id')->get();
        $store_ids = $MerchantStore->toArray();
        $head_store=DB::table('stores')->where('store_id', $store_ids[0]['store_id'])->first();

        $date=$this->getDateFromRange($starttime,$endtime);
        $service_day=DB::table('service_day_money')->whereIn('store_id', $store_ids)->whereIn('settling_day',$date)->orderBy('settling_day','desc')->get();
        $service_day_count=DB::table('service_day_money')->whereIn('store_id', $store_ids)->whereIn('settling_day',$date)->select('count')->sum('count');
        $return=array('service_day_count'=>$service_day_count,'service_day'=>$service_day,'store_name'=>$head_store->store_name);
        $this->status = 1;
        $this->message = '数据返回成功';
        return $this->format($return);
    }

    //服务费扣款记录
    public function getservermoney(Request $request){
        $merchant = $this->parseToken();
        $MerchantStore = MerchantStore::where('merchant_id', $merchant->merchant_id)->select('store_id')->get();
        $store_ids = $MerchantStore->toArray();
        $store123=DB::table('stores')->where('rata_status','1')->where('store_id',$store_ids[0]['store_id'])->where('pid','4377')->first();

        $settling_day = $request->get('settling_day', '');
        $obj = DB::table('orders');
        $where[] = ['created_at', '>=', $settling_day.' 00:00:00'];
        $where[] = ['created_at', '<=', $settling_day.' 23:59:59'];
        $where1[] = ['rata_money', '>', 0];
        $total_fee=DB::table('orders')->where($where)->whereIn('pay_status', [1, 6])->whereIn('store_id', $store_ids)->select('total_amount')->sum('total_amount');
        $total_num=DB::table('orders')->where($where)->whereIn('pay_status', [1, 6])->whereIn('store_id', $store_ids)->select('total_amount')->count();
        $server_fee=DB::table('orders')->where($where)->where($where1)->whereIn('pay_status', [1, 6])->whereIn('store_id', $store_ids)->select('rata_money')->sum('rata_money');
        $server_num=DB::table('orders')->where($where)->where($where1)->whereIn('pay_status', [1, 6])->whereIn('store_id', $store_ids)->select('rata_money')->count();
        $ali_fee=DB::table('orders')->where($where)->where($where1)->where('ways_source', 'alipay')->whereIn('pay_status', [1, 6])->whereIn('store_id', $store_ids)->select('rata_money')->sum('rata_money');
        $ali_num=DB::table('orders')->where($where)->where($where1)->where('ways_source', 'alipay')->whereIn('pay_status', [1, 6])->whereIn('store_id', $store_ids)->select('rata_money')->count();
        $wx_fee=DB::table('orders')->where($where)->where($where1)->where('ways_source', 'weixin')->whereIn('pay_status', [1, 6])->whereIn('store_id', $store_ids)->select('rata_money')->sum('rata_money');
        $wx_num=DB::table('orders')->where($where)->where($where1)->where('ways_source', 'weixin')->whereIn('pay_status', [1, 6])->whereIn('store_id', $store_ids)->select('rata_money')->count();
        $un_fee=DB::table('orders')->where($where)->where($where1)->where('ways_source', 'unionpay')->whereIn('pay_status', [1, 6])->whereIn('store_id', $store_ids)->select('rata_money')->sum('rata_money');
        $un_num=DB::table('orders')->where($where)->where($where1)->where('ways_source', 'unionpay')->whereIn('pay_status', [1, 6])->whereIn('store_id', $store_ids)->select('rata_money')->count();
        $obj = $obj->where($where)
            ->whereIn('store_id', $store_ids)
            ->whereIn('pay_status', [1, 6])
            ->orderBy('created_at', 'desc');
        if(!empty($store123)){
           $server_fee='当前扣费，请看外面';
        }
        $this->t = $obj->count();
        $data = $this->page($obj)->get();
        $this->order_data=array('total_fee'=>$total_fee,'total_num'=>$total_num,'server_fee'=>$server_fee,'server_num'=>$server_num,'ali_fee'=>$ali_fee,'ali_num'=>$ali_num,'wx_fee'=>$wx_fee,'wx_num'=>$wx_num,'un_fee'=>$un_fee,'un_num'=>$un_num);
        $this->status = 1;
        $this->message = '数据返回成功';

        return $this->format($data);
    }


    //经营数据统计-即将舍弃
    public function data_count(Request $request)
    {
        try {
            // dd($merchant->store['store_id']);
            $merchant = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $merchant_id = $request->get('merchant_id', '');
            $time_start = $request->get('time_start', '');
            $time_end = $request->get('time_end', '');
            $time_type = $request->get('time_type', '1'); //时间格式
            $store_ids = [];
            $check_data = [
                'time_start' => '开始时间',
                'time_end' => '结束时间',
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }
            //没有传入收银员id 角色是收银员 只返回自己
            if ($merchant->merchant_type == 2) {
                $merchant_id = $merchant->merchant_id;
            }

            //如果门店为空 传登录者绑定的门店ID
            if ($store_id) {
                $where[] = ['store_id', '=', $store_id];
                $store_ids = [
                    [
                        'store_id' => $store_id,
                    ]
                ];

            } else {
                $MerchantStore = MerchantStore::where('merchant_id', $merchant->merchant_id)
                    ->select('store_id')
                    ->get();
                if (!$MerchantStore->isEmpty()) {
                    $store_ids = $MerchantStore->toArray();
                }
            }

            if ($time_type == '1') {
                $y = date("Y", time());
                $time_start = $y . $time_start;
                $time_end = $y . $time_end;
            } else {
                $time_start = date("Ymd", strtotime($time_start));
                $time_end = date("Ymd", strtotime($time_end));
            }

            $time_start1 = date('Y-m-d H:i:s', strtotime($time_start));
            $time_end1 = date('Y-m-d 23:59:59', strtotime($time_end));


            if ($merchant->merchant_type == 2) {
                $data_obj = MerchantStoreDayOrder::whereBetween('day', [$time_start, $time_end])
                    ->where('merchant_id', $merchant_id)
                    ->whereIn('store_id', $store_ids)
                    ->select('total_amount', 'order_sum');

                $hb_obj = MerchantStoreDayOrder::whereBetween('day', [$time_start, $time_end])
                    ->where('merchant_id', $merchant_id)
                    ->whereIn('store_id', $store_ids)
                    ->select('total_amount', 'order_sum')
                    ->whereIn('type', [1006, 1007]);


                $refund_obj = RefundOrder::where('merchant_id', $merchant_id)
                    ->where('updated_at', '<=', $time_end1)
                    ->where('updated_at', '>=', $time_start1)
                    ->whereIn('store_id', $store_ids)
                    ->select('refund_amount');

                $alipay_obj = MerchantStoreDayOrder::whereBetween('day', [$time_start, $time_end])
                    ->where('merchant_id', $merchant_id)
                    ->whereIn('store_id', $store_ids)
                    ->select('total_amount', 'order_sum')
                    ->where('source_type', 'alipay');

                $weixin_obj = MerchantStoreDayOrder::whereBetween('day', [$time_start, $time_end])
                    ->where('merchant_id', $merchant_id)
                    ->whereIn('store_id', $store_ids)
                    ->select('total_amount', 'order_sum')
                    ->where('source_type', 'weixin');

                $jd_obj = MerchantStoreDayOrder::whereBetween('day', [$time_start, $time_end])
                    ->where('merchant_id', $merchant_id)
                    ->whereIn('store_id', $store_ids)
                    ->select('total_amount', 'order_sum')
                    ->where('source_type', 'jdjr');


            } else {


                $data_obj = StoreDayOrder::whereBetween('day', [$time_start, $time_end])
                    ->whereIn('store_id', $store_ids)
                    ->select('total_amount', 'order_sum');


                $hb_obj = StoreDayOrder::whereBetween('day', [$time_start, $time_end])
                    ->whereIn('store_id', $store_ids)
                    ->select('total_amount', 'order_sum')
                    ->whereIn('type', [1006, 1007]);

                $refund_obj = RefundOrder::where('updated_at', '<=', $time_end1)
                    ->where('updated_at', '>=', $time_start1)
                    ->whereIn('store_id', $store_ids)
                    ->select('refund_amount');


                $alipay_obj = StoreDayOrder::whereBetween('day', [$time_start, $time_end])
                    ->whereIn('store_id', $store_ids)
                    ->select('total_amount', 'order_sum')
                    ->where('source_type', 'alipay');

                $weixin_obj = StoreDayOrder::whereBetween('day', [$time_start, $time_end])
                    ->whereIn('store_id', $store_ids)
                    ->select('total_amount', 'order_sum')
                    ->where('source_type', 'weixin');

                $jd_obj = StoreDayOrder::whereBetween('day', [$time_start, $time_end])
                    ->whereIn('store_id', $store_ids)
                    ->select('total_amount', 'order_sum')
                    ->where('source_type', 'jdjr');


            }

            //总金额

            $total_amount = $data_obj->sum('total_amount');
            $order_sum = $data_obj->sum('order_sum');

            $hb_fq_amount = $hb_obj->sum('total_amount');
            $hb_fq_count = $hb_obj->sum('order_sum');

            $alipay_amount = $alipay_obj->sum('total_amount');
            $alipay_count = $alipay_obj->sum('order_sum');

            $weixin_amount = $weixin_obj->sum('total_amount');
            $weixin_count = $weixin_obj->sum('order_sum');

            $jd_amount = $jd_obj->sum('total_amount');
            $jd_count = $jd_obj->sum('order_sum');


            $refund_amount = $refund_obj->sum('refund_amount');
            $refund_count = count($refund_obj->get());


            $data = [
                'total_amount' => number_format($total_amount, 2, '.', ''),
                'total_count' => "" . $order_sum . "",

                'refund_amount' => number_format($refund_amount, 2, '.', ''),
                'refund_count' => "" . $refund_count . "",

                'alipay_amount' => number_format($alipay_amount, 2, '.', ''),
                'alipay_count' => "" . $alipay_count . "",

                'weixin_amount' => number_format($weixin_amount, 2, '.', ''),
                'weixin_count' => "" . $weixin_count . "",

                'jd_amount' => number_format($jd_amount, 2, '.', ''),
                'jd_count' => '' . $jd_count . '',

                'hb_fq_amount' => number_format($hb_fq_amount, 2, '.', ''),
                'hb_fq_count' => "" . $hb_fq_count . "",

            ];
            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage() . $exception->getLine();
            return $this->format();
        }
    }


    //数据走势
    public function order_data(Request $request)
    {
        try {
            // dd($merchant->store['store_id']);
            $merchant = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $merchant_id = $request->get('merchant_id', '');

            //如果门店为空 传登录者绑定的门店ID
            if ($store_id == "") {
                $MyBankStore = MerchantStore::where('merchant_id', $merchant->merchant_id)
                    ->orderBy('created_at', 'asc')
                    ->first();
                $store_id = $MyBankStore->store_id;
            }
            $store_ids = [$store_id];

            //没有传入收银员id 角色是收银员 只返回自己
            if ($merchant->merchant_type == 2) {
                $merchant_id = $merchant->merchant_id;
            } else {
                $where[] = ['store_id', '=', $store_id];
                $store = Store::where('store_id', $store_id)
                    ->select('id', 'pid')
                    ->first();

                if ($store) {
                    $store_ids = $this->getStore_id($store_id, $store->id);
                }
            }


            //今日
            $day = date('Ymd', time());
            $beginToday = date("Y-m-d 00:00:00", time());
            $endToday = date("Y-m-d H:i:s", time());


            //收银员
            if ($merchant->merchant_type == 2) {
                $day_order_data = Order::whereIn('store_id', $store_ids)
                    ->where('created_at', '>=', $beginToday)
                    ->where('merchant_id', $merchant_id)
                    ->where('created_at', '<=', $endToday)
                    ->whereIn('pay_status', [1, 3, 6])
                    ->select('total_amount');

                $refund_order_data = RefundOrder::whereBetween('created_at', [$beginToday, $endToday])
                    ->where('merchant_id', $merchant_id)
                    ->where('store_id', $store_id)
                    ->select('refund_amount');
            } else {
                $day_order_data = Order::whereIn('store_id', $store_ids)
                    ->where('created_at', '>=', $beginToday)
                    ->where('created_at', '<=', $endToday)
                    ->whereIn('pay_status', [1, 3, 6])
                    ->select('total_amount');

                $refund_order_data = RefundOrder::whereBetween('created_at', [$beginToday, $endToday])
                    ->whereIn('store_id', $store_ids)
                    ->select('refund_amount');
            }

            $day_order = $day_order_data->sum('total_amount');
            $day_order = '' . $day_order . '';
            $day_order_count = '' . count($day_order_data->get()) . '';

            $refund_day_order = $refund_order_data->sum('refund_amount');
            $refund_day_order = '' . $refund_day_order . '';
            $refund_day_order_count = '' . count($refund_order_data->get()) . '';

            //上个月
            //得到系统的年月
            $tmp_date = date("Ym", time());
            //切割出年份
            $tmp_year = substr($tmp_date, 0, 4);
            //切割出月份
            $tmp_mon = substr($tmp_date, 4, 2);
            $tmp_forwardmonth = mktime(0, 0, 0, $tmp_mon - 1, 1, $tmp_year);
            $fm_forward_month = date("Ym", $tmp_forwardmonth);

            $old_begin_time = date("Y-m-d 00:00:00", $tmp_forwardmonth);
            $old_end_time = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day'));

            if ($merchant->merchant_type == 2) {
                $old_month_order_data = Order::whereIn('store_id', $store_ids)
                    ->where('created_at', '>=', $old_begin_time)
                    ->where('merchant_id', $merchant_id)
                    ->where('created_at', '<=', $old_end_time)
                    ->whereIn('pay_status', [1, 3, 6])
                    ->select('total_amount');

                $refund_old_month_order_data = RefundOrder::whereBetween('created_at', [$old_begin_time, $old_end_time])
                    ->where('merchant_id', $merchant_id)
                    ->where('store_id', $store_id)
                    ->select('refund_amount');
            } else {
                $old_month_order_data = Order::whereIn('store_id', $store_ids)
                    ->where('created_at', '>=', $old_begin_time)
                    ->where('created_at', '<=', $old_end_time)
                    ->whereIn('pay_status', [1, 3, 6])
                    ->select('total_amount');

                $refund_old_month_order_data = RefundOrder::whereBetween('created_at', [$old_begin_time, $old_end_time])
                    ->whereIn('store_id', $store_ids)
                    ->select('refund_amount');
            }

            $old_month_order = $old_month_order_data->sum('total_amount');
            $old_month_amount = '' . $old_month_order . '';
            $old_month_count = '' . count($old_month_order_data->get()) . '';

            $refund_old_month_amount = $refund_old_month_order_data->sum('refund_amount');
            $refund_old_month_amount = '' . $refund_old_month_amount . '';
            $refund_old_month_count = '' . count($refund_old_month_order_data->get()) . '';

            //本月
            $tmonth = date('Ym', time());
            $month_begin_time = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), 1, date("Y")));
            $month_end_time = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("t"), date("Y")));

            if ($merchant->merchant_type == 2) {
                $month_order_data = Order::whereIn('store_id', $store_ids)
                    ->where('created_at', '>=', $month_begin_time)
                    ->where('merchant_id', $merchant_id)
                    ->where('created_at', '<=', $month_end_time)
                    ->whereIn('pay_status', [1, 3, 6])
                    ->select('total_amount');

                $refund_month_order_data = RefundOrder::whereBetween('created_at', [$month_begin_time, $month_end_time])
                    ->where('merchant_id', $merchant_id)
                    ->where('store_id', $store_id)
                    ->select('refund_amount');
            } else {
                $month_order_data = Order::whereIn('store_id', $store_ids)
                    ->where('created_at', '>=', $month_begin_time)
                    ->where('created_at', '<=', $month_end_time)
                    ->whereIn('pay_status', [1, 3, 6])
                    ->select('total_amount');

                $refund_month_order_data = RefundOrder::whereBetween('created_at', [$month_begin_time, $month_end_time])
                    ->whereIn('store_id', $store_ids)
                    ->select('refund_amount');
            }

            $month_order = $month_order_data->sum('total_amount');
            $month_amount = '' . $month_order . '';
            $month_count = '' . count($month_order_data->get()) . '';

            $refund_month_amount = $refund_month_order_data->sum('refund_amount');
            $refund_month_amount = '' . $refund_month_amount . '';
            $refund_month_count = '' . count($refund_month_order_data->get()) . '';

            //7天
            $data_day = [];
            $data = [1, 2, 3, 4, 5, 6, 7];
            foreach ($data as $k => $v) {
                $day = date("Ymd", time() - 24 * $v * 60 * 60);
                $data_day[$k]['date'] = date("m/d", time() - 24 * $v * 60 * 60);

                if ($merchant_id) {
                    $day_order_data = MerchantStoreDayOrder::where('day', $day)
                        ->where('merchant_id', $merchant_id)
                        ->select('total_amount', 'order_sum');
                } else {
                    $day_order_data = StoreDayOrder::where('day', $day)
                        ->whereIn('store_id', $store_ids);
                }

                $day_order_day = $day_order_data->sum('total_amount');
                $day_order_day = '' . $day_order_day . '';
                $day_order_count_day = '' . $day_order_data->sum('order_sum') . '';
                $data_day[$k]['day_amount'] = number_format($day_order_day, 2, '.', '');
                $data_day[$k]['day_count'] = $day_order_count_day;
            }

            $data = [
                'day_amount' => number_format($day_order, 2, '.', ''),
                'day_count' => $day_order_count,

                'refund_day_amount' => number_format($refund_day_order, 2, '.', ''),
                'refund_day_count' => $refund_day_order_count,

                'month_amount' => number_format($month_amount, 2, '.', ''),
                'month_count' => $month_count,

                'refund_month_amount' => number_format($refund_month_amount, 2, '.', ''),
                'refund_month_count' => $refund_month_count,

                'old_month_amount' => number_format($old_month_amount, 2, '.', ''),
                'old_month_count' => $old_month_count,

                'refund_old_month_amount' => number_format($refund_old_month_amount, 2, '.', ''),
                'refund_old_month_count' => $refund_old_month_count,

                'data_day' => $data_day
            ];

            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage() . $exception->getLine();
            return $this->format();
        }
    }


    //APP订单轮询接口
    public function order_foreach(Request $request)
    {
        try {
            $store_id = $request->get('store_id', '');
            $out_trade_no = $request->get('out_trade_no', '');
            $ways_type = $request->get('ways_type', '');
            $config_id = $request->get('config_id', '');

            $data = [
                'out_trade_no' => $out_trade_no,
                'store_id' => $store_id,
                'ways_type' => $ways_type,
                'config_id' => $config_id,

            ];
            return $this->order_foreach_public($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage() . $exception->getLine();
            return $this->format();
        }
    }


    //APP订单轮询-公共
    public function order_foreach_public($data)
    {
        try {
            $store_id = $data['store_id'];
            $out_trade_no = isset($data['out_trade_no']) ? $data['out_trade_no'] : "";
            $other_no = isset($data['other_no']) ? $data['other_no'] : "";
            $table = isset($data['table']) ? $data['table'] : "";

            $ways_type = $data['ways_type'];
            $config_id = $data['config_id'];
            if ($out_trade_no) {
                $c_o = 'a' . $out_trade_no;
                $where[] = ['out_trade_no', '=', $out_trade_no];
            } elseif($other_no) {
                $c_o = 'a' . $other_no;
                $where[] = ['other_no', '=', $other_no];
            } else {
                $c_o = 'a' . $out_trade_no;
                $where[] = ['auth_code', '=', $out_trade_no];
            }

            if ($table == "") {
                $day = date('Ymd', time());
                $table = 'orders_' . $day;
            }

            $order = Cache::get($c_o);
            if (!$order) {
                if (Schema::hasTable($table)) {
                    $order = DB::table($table)->where('store_id', $store_id);
                } else {
                    $order = Order::where('store_id', $store_id);
                }
                //发起查询
                $order = $order->where($where)->first();
                Cache::put($c_o, $order, 1);
            }
            if (!$order) {
                $order = Order::where('out_trade_no', $out_trade_no)->first();
            }
            if (!$order) {
                if (isset($data['channel_code']) && $data['channel_code'] == 'jiujiang') {
                    $orderExtend = \App\Models\OrderExtend::where('transactionId', $out_trade_no)->where('channel_code', 1)->first();
                    if ($orderExtend) {
                        $order = Order::where('out_trade_no', $orderExtend->out_trade_no)->first();
                    }
                }
            }
            
            if (!$order) {
                if (Schema::hasTable($table)) {
                    $order = DB::table($table)->where('qwx_no', $out_trade_no);
                } else {
                    $order = Order::where('qwx_no', $out_trade_no);
                }
                $order = $order->first();
            }
            
             if (isset($data['sxf_uuid']) && !empty($data['sxf_uuid'])) {
                $orderExtend = \App\Models\OrderExtend::where('user_bar_code', $data['sxf_uuid'])->where('channel_code', 1)->first();
                if (!isset($orderExtend->out_trade_no)) {
                    return json_encode([
                        'status' => 2,
                        'message' => '订单号不存在'
                    ]);
                }
                $out_trade_no = $orderExtend->out_trade_no;
                if (Schema::hasTable($table)) {
                    $order = DB::table($table)->where('out_trade_no', $out_trade_no)->where('store_id', $store_id);
                } else {
                    $order = Order::where('out_trade_no', $out_trade_no)->where('store_id', $store_id);
                }
                $order = $order->first();
            }
            
            
            if (!$order) {
                return json_encode([
                    'status' => 2,
                    'message' => '订单号不存在'
                ]);
            }
            $ways_type = $order->ways_type;
            $out_trade_no = $order->out_trade_no;
            $other_no = $order->other_no;
            $created_at = $order->created_at;
            $store = Store::where('store_id', $store_id)
                ->select("store_id",'config_id', 'merchant_id', 'pid')
                ->first();

            if (!$store) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店ID不存在'
                ]);
            }
            
            if (isset($order->pay_status) && $order->pay_status == 6) {
                return json_encode([
                    'status' => 1,
                    'pay_status' => '6',
                    'message' => '订单已经退款',
                    'data' => [
                        'ways_source' => $order->ways_source,
                        'out_trade_no' => $order->out_trade_no,
                        'pay_amount' => $order->pay_amount,
                        'total_amount' => $order->total_amount
                    ],
                ]);
            }
            
            $config_id = $store->config_id;
            $store_pid = $store->pid;

            // 国通支付 交易查询  gtxy  已新增v2
            if(in_array($ways_type,[62001,62002,62004,62011,62012,62014])){
                return BizService::gtxy_query($order,$store,$c_o,$this);
            }


            // 九江 交易查询
            if (36999 < $ways_type && $ways_type < 37999) {
                $config = new JiuJiangConfigController();
                $jiujiang_merchant = $config->jiujiang_merchant($store_id, $store_pid);
                if (!$jiujiang_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '九江银行商户号不存在'
                    ]);
                }
                $txnAmt = substr( strval($order->total_amount * 100 + 1000000000000), 1, 15 ); // 交易金额（分，12 位，不足左补 0）
                $isGateway = $order->device_id == 'app';
                if($isGateway){
                    $postdate = [
                        'mchtNo' => $jiujiang_merchant->mchtNo, // 平台分配的商户号
                        'signType' => "RSA",
                        'outOrderNo' => $out_trade_no, 
                    ];
                }else{
                    $postdate = [
                        'requestBranchCode' => '0200410000032',
                        'mchtNo' => $jiujiang_merchant->mchtNo, // 平台分配的商户号
                        'termNo' => $jiujiang_merchant->termNo, // 平台分配的终端号
                        'secMchtNo' => $jiujiang_merchant->mchtNo, 
                        'oriTxnDate' => date('Ymd', strtotime($order->created_at)), 
                        'txnAmt' => $txnAmt, // 交易金额（分，12 位，不足左补 0）
                        'outOrderNo' => $out_trade_no, 
                        'ordrNoReturnFlag' => '1', // 返回支付凭证条码
                    ];
                }
                $obj = new \App\Api\Controllers\JiuJiang\PayController();
                Log::channel("company")->info(date('Y-m-d H:i:s')." jiujiang-query-req-".$out_trade_no,[
                    "reqDate"=>$postdate,
                ]);
                $result = $obj->order_query($postdate,$jiujiang_merchant->private_key);
                Log::channel("company")->info(date('Y-m-d H:i:s')." jiujiang-query-res-".$out_trade_no,[
                    "resDate"=>$result,
                ]);
                //支付成功
                if ($result['pay_status'] == '1') {
                    
                    $pay_time = date('Y-m-d H:i:s', time()); //交易时间 格式：MMDDhhmmss
                    
                    if (isset($result['data']['payEndTime']) && !empty($result['data']['payEndTime'])) {
                        $pay_time = date('Y-m-d H:i:s', strtotime($result['data']['payEndTime']));
                    }
                    
                    
                    $trade_no = $result['data']['payNumber']; // 九江的流水号
                    
                    // $buyer_pay_amount = $result['data']['total_amount'] ? ($result['data']['total_amount']/100): ''; //订单金额 (打印小票使用)
                    $buyer_pay_amount = $order->total_amount; //订单金额
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                    $depBankSeq = $other_no ?? ''; //第三方流水号（微信/支付宝/银联的交易流水号）
                    
                    $qwxNo = '';
                    if ($jiujiang_merchant->mchtNo == '45436105462C030') {
                        $qwxNo = $result['data']['transactionId'];
                        
                    }
                    
                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);
                        
                        
                         //添加扩展数据
                        \App\Models\OrderExtend::create([
                            'out_trade_no' => $out_trade_no,
                            'transactionId' => isset($result['data']['transactionId']) ? $result['data']['transactionId'] : '',
                            'user_bar_code' => isset($result['data']['ordrNo']) ? $result['data']['ordrNo'] : '',
                        ]);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'qwx_no' => $qwxNo,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                            'other_no' => $depBankSeq
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '5000', //返佣来源
                            'source_desc' => '联动优势', //返佣来源说明
                            'total_amount' => $buyer_pay_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no,
                            'qwx_no' => $qwxNo
                        ]
                    ]);
                }else if($result['pay_status'] == '2') {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => '2',
                        'message' => '支付中',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
                else {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => 3,
                        'message' => $result['message'],
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }
            
            //支付宝官方订单
            if (999 < $ways_type && $ways_type < 1999) {
                //配置
                $isvconfig = new AlipayIsvConfigController();
                $storeInfo = $isvconfig->alipay_auth_info($store_id, $store_pid);
                $config = $isvconfig->AlipayIsvConfig($config_id);

                $app_auth_token = $storeInfo->app_auth_token;

                $notify_url = url('/api/alipayopen/qr_pay_notify');
                $aop = new AopClient();
                $aop->apiVersion = "2.0";
                $aop->appId = $config->app_id;
                $aop->rsaPrivateKey = $config->rsa_private_key;
                $aop->alipayrsaPublicKey = $config->alipay_rsa_public_key;
                $aop->notify_url = $notify_url;
                $aop->signType = "RSA2"; //升级算法
                $aop->gatewayUrl = $config->alipay_gateway;
                $aop->format = "json";
                $aop->charset = "GBK";
                $aop->version = "2.0";
                $aop->method = 'alipay.trade.query';
                $requests = new AlipayTradeQueryRequest();
                $requests->setBizContent("{" .
                    "    \"out_trade_no\":\"" . $out_trade_no . "\"" .
                    "  }");
                $status = $aop->execute($requests, '', $app_auth_token);


                if ($status->alipay_trade_query_response->code == 40004) {


                    $is_dqr = substr($out_trade_no, 0, 3);
                    $pay_status = "3";
                    $re_status = 2;
                    //动态码在没有付款会报交易不存在
                    if ($is_dqr == "DQR") {
                        $pay_status = "2";
                        $re_status = 1;
                        return json_encode([
                            'status' => $re_status,
                            'pay_status' => $pay_status,
                            'message' => $status->alipay_trade_query_response->sub_msg,
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount,
                                'other_no' => $other_no,

                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => $re_status,
                            'pay_status' => $pay_status,
                            'message' => $status->alipay_trade_query_response->sub_msg,
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount,
                            ]
                        ]);
                    }

                }
                //支付成功
                if ($status->alipay_trade_query_response->trade_status == "TRADE_SUCCESS") {
                    $buyer_pay_amount = $status->alipay_trade_query_response->buyer_pay_amount;

                    //改变数据库状态
                    if ($order->pay_status != 1) {

                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => 'TRADE_SUCCESS',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => $status->alipay_trade_query_response->buyer_user_id,
                            'trade_no' => $status->alipay_trade_query_response->trade_no,
                            'pay_time' => $status->alipay_trade_query_response->send_pay_date,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $order->out_trade_no, $table);


                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '1000',//返佣来源
                            'source_desc' => '支付宝',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $status->alipay_trade_query_response->send_pay_date,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",

                        ];


                        PaySuccessAction::action($data);


                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'trade_no' => $status->alipay_trade_query_response->trade_no,
                            'pay_time' => $status->alipay_trade_query_response->send_pay_date,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,

                        ]
                    ]);

                } //等待付款
                elseif ($status->alipay_trade_query_response->trade_status == "WAIT_BUYER_PAY") {

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);

                } //订单关闭
                elseif ($status->alipay_trade_query_response->trade_status == 'TRADE_CLOSED') {
                    $order->update([
                        'status' => '4',
                        'pay_status' => 4,
                        'pay_status_desc' => '订单关闭',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                    $order->save();

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $status->alipay_trade_query_response->sub_msg;
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                }
            }

            //直付通
            if (16000 < $ways_type && $ways_type < 16999) {
                $config_type = '03';
                //配置
                $isvconfig = new AlipayIsvConfigController();
                $config = $isvconfig->AlipayIsvConfig($config_id, $config_type);

                $notify_url = url('/api/alipayopen/qr_pay_notify');
                $aop = new AopClient();
                $aop->apiVersion = "2.0";
                $aop->appId = $config->app_id;
                $aop->rsaPrivateKey = $config->rsa_private_key;
                $aop->alipayrsaPublicKey = $config->alipay_rsa_public_key;
                $aop->notify_url = $notify_url;
                $aop->signType = "RSA2"; //升级算法
                $aop->gatewayUrl = $config->alipay_gateway;
                $aop->format = "json";
                $aop->charset = "GBK";
                $aop->version = "2.0";
                $aop->method = 'alipay.trade.query';
                $requests = new AlipayTradeQueryRequest();
                $requests->setBizContent("{" .
                    "    \"out_trade_no\":\"" . $out_trade_no . "\"" .
                    "  }");
                $status = $aop->execute($requests, '', '');

                if ($status->alipay_trade_query_response->code == 40004) {
                    $is_dqr = substr($out_trade_no, 0, 3);
                    $pay_status = "3";
                    $re_status = 2;
                    //动态码在没有付款会报交易不存在
                    if ($is_dqr == "DQR") {
                        $pay_status = "2";
                        $re_status = 1;
                        return json_encode([
                            'status' => $re_status,
                            'pay_status' => $pay_status,
                            'message' => $status->alipay_trade_query_response->sub_msg,
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount,
                                'other_no' => $other_no,
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => $re_status,
                            'pay_status' => $pay_status,
                            'message' => $status->alipay_trade_query_response->sub_msg,
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount,
                            ]
                        ]);
                    }
                }
                //支付成功
                if ($status->alipay_trade_query_response->trade_status == "TRADE_SUCCESS") {
                    $buyer_pay_amount = $status->alipay_trade_query_response->buyer_pay_amount;

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);
                        $insert_data = [
                            'status' => 'TRADE_SUCCESS',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => $status->alipay_trade_query_response->buyer_user_id,
                            'trade_no' => $status->alipay_trade_query_response->trade_no,
                            'pay_time' => $status->alipay_trade_query_response->send_pay_date,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $order->out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '16000',//返佣来源
                            'source_desc' => '支付宝',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $status->alipay_trade_query_response->send_pay_date,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'trade_no' => $status->alipay_trade_query_response->trade_no,
                            'pay_time' => $status->alipay_trade_query_response->send_pay_date,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no
                        ]
                    ]);
                } //等待付款
                elseif ($status->alipay_trade_query_response->trade_status == "WAIT_BUYER_PAY") {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                } //订单关闭
                elseif ($status->alipay_trade_query_response->trade_status == 'TRADE_CLOSED') {
                    $order->update([
                        'status' => '4',
                        'pay_status' => 4,
                        'pay_status_desc' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                    $order->save();

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $status->alipay_trade_query_response->sub_msg;
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }

            //官方微信支付订单
            if (1999 < $ways_type && $ways_type < 2999) {
                $config = new WeixinConfigController();
                $options = $config->weixin_config($config_id);
                $weixin_store = $config->weixin_merchant($store_id, $store_pid);
                $wx_sub_merchant_id = $weixin_store->wx_sub_merchant_id;
                $config = [
                    'app_id' => $options['app_id'],
                    'mch_id' => $options['payment']['merchant_id'],
                    'key' => $options['payment']['key'],
                    'cert_path' => $options['payment']['cert_path'], // XXX: 绝对路径！！！！
                    'key_path' => $options['payment']['key_path'],     // XXX: 绝对路径！！！！
                    'sub_mch_id' => $wx_sub_merchant_id,
                    // 'device_info'     => '013467007045764',
                    // 'sub_app_id'      => '',
                    // ...
                ];

                $payment = Factory::payment($config);
                $query = $payment->order->queryByOutTradeNumber($order->out_trade_no);

                //成功
                if ($query['trade_state'] == 'SUCCESS') {
                    $re_data['result_code'] = 'SUCCESS';
                    $re_data['result_msg'] = '支付成功';
                    $re_data['outTradeNo'] = $order->qwx_no;
                    $re_data['out_transaction_id'] = $order->out_trade_no; //$query['transaction_id'];
                    $re_data['time_end'] = $query['time_end'];

                    if ($order->pay_status != 1) {
                        //改变数据库状态
                        Cache::forget($c_o);
                        $insert_data = [
                            'status' => 'TRADE_SUCCESS',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => $query['openid'],
                            'trade_no' => $query['transaction_id'],
                            'pay_time' => date('Y-m-d H:i:s', strtotime($query['time_end']))
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '2000',//返佣来源
                            'source_desc' => '微信支付',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => date('Y-m-d H:i:s', strtotime($query['time_end'])),
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => date('Y-m-d H:i:s', strtotime($query['time_end'])),
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $query['transaction_id']
                        ]
                    ]);
                } elseif ($query['trade_state'] == "USERPAYING" || $query['trade_state'] == "NOTPAY") {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no
                        ]
                    ]);
                } elseif ($query['trade_state'] == "CLOSED") {
                    $order->update([
                        'status' => '4',
                        'pay_status' => '4',
                        'pay_status_desc' => '订单关闭',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                    $order->save();

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } elseif ($query['trade_state'] == "REVOKED") {
                    $order->update([
                        'status' => '3',
                        'pay_status' => '3',
                        'pay_status_desc' => '订单已撤销',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                    $order->save();

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $query['trade_state_desc'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }

            //官方微信a 支付订单
            if (3999 < $ways_type && $ways_type < 4999) {
                $config = new WeixinConfigController();
                $options = $config->weixina_config($config_id);
                $weixin_store = $config->weixina_merchant($store_id, $store_pid);
                $wx_sub_merchant_id = $weixin_store->wx_sub_merchant_id;
                $config = [
                    'app_id' => $options['app_id'],
                    'mch_id' => $options['payment']['merchant_id'],
                    'key' => $options['payment']['key'],
                    'cert_path' => $options['payment']['cert_path'], // XXX: 绝对路径！！！！
                    'key_path' => $options['payment']['key_path'],     // XXX: 绝对路径！！！！
                    'sub_mch_id' => $wx_sub_merchant_id,
                    // 'device_info'     => '013467007045764',
                    // 'sub_app_id'      => '',
                    // ...
                ];
                $payment = Factory::payment($config);
                $query = $payment->order->queryByOutTradeNumber($order->out_trade_no);

                //成功
                if ($query['trade_state'] == 'SUCCESS') {
                    $re_data['result_code'] = 'SUCCESS';
                    $re_data['result_msg'] = '支付成功';
                    $re_data['outTradeNo'] = $order->qwx_no;
                    $re_data['out_transaction_id'] = $order->out_trade_no; //$query['transaction_id'];
                    $re_data['time_end'] = $query['time_end'];

                    if ($order->pay_status != 1) {
                        //改变数据库状态
                        Cache::forget($c_o);
                        $insert_data = [
                            'status' => 'TRADE_SUCCESS',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => $query['openid'],
                            'trade_no' => $query['transaction_id'],
                            'pay_time' => date('Y-m-d H:i:s', strtotime($query['time_end']))
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '4000', //返佣来源
                            'source_desc' => '微信支付a', //返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => date('Y-m-d H:i:s', strtotime($query['time_end'])),
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => date('Y-m-d H:i:s', strtotime($query['time_end'])),
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $query['transaction_id']
                        ]
                    ]);
                } elseif ($query['trade_state'] == "USERPAYING" || $query['trade_state'] == "NOTPAY") {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no
                        ]
                    ]);
                } elseif ($query['trade_state'] == "CLOSED") {
                    $order->update([
                        'status' => '4',
                        'pay_status' => '4',
                        'pay_status_desc' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                    $order->save();

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } elseif ($query['trade_state'] == "REVOKED") {
                    $order->update([
                        'status' => '3',
                        'pay_status' => '3',
                        'pay_status_desc' => '订单已撤销',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                    $order->save();

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $query['trade_state_desc'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }

            //京东收银支付
            if (5999 < $ways_type && $ways_type < 6999) {
                //读取配置
                $config = new JdConfigController();
                $jd_config = $config->jd_config($config_id);
                if (!$jd_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '京东配置不存在请检查配置'
                    ]);
                }

                $jd_merchant = $config->jd_merchant($store_id, $store_pid);
                if (!$jd_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '京东商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Jd\PayController();
                $data = [];
                $data['out_trade_no'] = $out_trade_no;
                $data['request_url'] = $obj->order_query_url; //请求地址;
                $data['merchant_no'] = $jd_merchant->merchant_no;
                $data['md_key'] = $jd_merchant->md_key; //
                $data['des_key'] = $jd_merchant->des_key; //
                $data['systemId'] = $jd_config->systemId; //
                $return = $obj->order_query($data);
                //支付成功
                if ($return["status"] == 1) {
                    $pay_time = date('Y-m-d H:i:s', strtotime($return['data']['payFinishTime']));
                    $trade_no = $return['data']['tradeNo'];
                    $channelNoSeq = isset($return['data']['channelNoSeq']) ? $return['data']['channelNoSeq'] : $trade_no; //条码
                    $buyer_pay_amount = $return['data']['piAmount'] / 100;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $channelNoSeq,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '6000',//返佣来源
                            'source_desc' => '京东金融',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : ""
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $channelNoSeq
                        ]
                    ]);

                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no
                        ]
                    ]);
                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);

                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }

            //快钱支付
            if (2999 < $ways_type && $ways_type < 3999) {
                //读取配置
                $MyBankobj = new MyBankConfigController();
                $MyBankConfig = $MyBankobj->MyBankConfig($config_id);
                if (!$MyBankConfig) {
                    return json_encode([
                        'status' => 2,
                        'message' => '快钱配置不存在请检查配置'
                    ]);
                }

                $mybank_merchant = $MyBankobj->mybank_merchant($store_id, $store_pid);
                if (!$mybank_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '快钱商户号不存在'
                    ]);
                }

                $MerchantId = $mybank_merchant->MerchantId;
                $obj = new TradePayController();
                $return = $obj->mybankOrderQuery($MerchantId, $config_id, $out_trade_no);
                if ($return['status'] == 0) {
                    return json_encode([
                        'status' => 2,
                        'message' => $return['message']
                    ]);
                }
                $body = $return['data']['document']['response']['body'];
                $TradeStatus = $body['TradeStatus'];

                //成功
                if ($TradeStatus == 'succ') {
                    $OrderNo = $body['MerchantOrderNo'];
                    $GmtPayment = $body['GmtPayment'];
                    $buyer_id = '';
                    if ($ways_type == 3004) {
                        $buyer_id = $body['SubOpenId'];
                    }
                    if ($ways_type == 3003) {
                        $buyer_id = $body['BuyerUserId'];
                    }

                    $pay_time = date('Y-m-d H:i:s', strtotime($GmtPayment));
                    $payment_method = strtolower($body['Credit']);

                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);
                        $insert_data = [
                            'status' => 1,
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_id' => $buyer_id,
                            'trade_no' => $OrderNo,
                            'pay_time' => $pay_time,
                            'payment_method' => $payment_method,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '3000',//返佣来源
                            'source_desc' => '快钱支付',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : ""
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $OrderNo
                        ]
                    ]);
                } elseif ($TradeStatus == 'paying') {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = '请重新扫码';
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }

            //新大陆
            if (7999 < $ways_type && $ways_type < 8999) {
                //读取配置
                $config = new NewLandConfigController();
                $new_land_config = $config->new_land_config($config_id);
                if (!$new_land_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '新大陆配置不存在请检查配置'
                    ]);
                }

                $mybank_merchant = $config->new_land_merchant($store_id, $store_pid);
                if (!$mybank_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户新大陆通道未开通'
                    ]);
                }

                $request_data = [
                    'out_trade_no' => $out_trade_no,
                    'key' => $mybank_merchant->nl_key,
                    'org_no' => $new_land_config->org_no,
                    'merc_id' => $mybank_merchant->nl_mercId,
                    'trm_no' => $mybank_merchant->trmNo,
                    'op_sys' => '3',
                    'opr_id' => $store->merchant_id,
                    'trm_typ' => 'T'
                ];

                $obj = new PayController();
                $return = $obj->order_query($request_data);
                //支付成功
                if ($return["status"] == 1) {
                    $pay_time = date('Y-m-d H:i:s', strtotime($return['data']['sysTime']));
                    $trade_no = $return['data']['orderNo'];
                    $buyer_pay_amount = $return['data']['amount'] / 100;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '8000',//返佣来源
                            'source_desc' => '新大陆',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : ""
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no
                        ]
                    ]);
                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }

            //和融通收银支付
            if (8999 < $ways_type && $ways_type < 9999) {
                //读取配置
                $config = new HConfigController();
                $h_config = $config->h_config($config_id);
                if (!$h_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '和融通不存在请检查配置'
                    ]);
                }

                $h_merchant = $config->h_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '和融通商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Huiyuanbao\PayController();
                $data = [];
                $data['out_trade_no'] = $out_trade_no;
                $data['request_url'] = $obj->order_query_url; //请求地址;
                $data['md_key'] = $h_config->md_key; //
                $data['mid'] = $h_merchant->h_mid; //
                $data['orgNo'] = $h_config->orgNo; //

                $return = $obj->order_query($data);

                //支付成功
                if ($return["status"] == 1) {
                    // $pay_time = strtotime($return['data']['timeEnd']) ? strtotime($return['data']['timeEnd']) : time();
                    $pay_time = date('Y-m-d H:i:s', time());
                    $trade_no = '112121' . $return['data']['transactionId'];
                    $buyer_pay_amount = $return['data']['totalFee'];
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '9000',//返佣来源
                            'source_desc' => '和融通',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : ""
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,

                        ]
                    ]);
                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);

                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }

            //ltf收银支付
            if (9999 < $ways_type && $ways_type < 10999) {
                //读取配置
                $config = new LtfConfigController();
                $ltf_merchant = $config->ltf_merchant($store_id, $store_pid);
                if (!$ltf_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Ltf\PayController();
                $data = [];
                $data['out_trade_no'] = $out_trade_no;
                $data['request_url'] = $obj->order_query_url; //请求地址;
                $data['merchant_no'] = $ltf_merchant->merchantCode;
                $data['appId'] = $ltf_merchant->appId; //
                $data['key'] = $ltf_merchant->md_key; //

                $return = $obj->order_query($data);
                //支付成功
                if ($return["status"] == 1) {
                    $pay_time = date('Y-m-d H:i:s', strtotime($return['data']['payTime']));
                    $trade_no = $return['data']['outTransactionId'];
                    $buyer_pay_amount = $return['data']['receiptAmount'];
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '10000',//返佣来源
                            'source_desc' => '联拓富',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : ""
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'other_no' => $other_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ],
                    ]);
                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ],
                    ]);
                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ],
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ],
                    ]);
                }
            }

            //随行付收银支付
            if (12999 < $ways_type && $ways_type < 13999) {
                //读取配置
                $config = new VbillConfigController();
                $vbill_config = $config->vbill_config($config_id);
                if (!$vbill_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '随行付配置不存在请检查配置'
                    ]);
                }
                $vbill_merchant = $config->vbill_merchant($store_id, $store_pid);
                if (!$vbill_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '随行付商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Vbill\PayController();
                $data['notify_url'] = url('/api/vbill/pay_notify_url'); //回调地址
                $data['request_url'] = $obj->order_query_url; //请求地址;
                $data['mno'] = $vbill_merchant->mno;
                $data['privateKey'] = $vbill_config->privateKey; //
                $data['sxfpublic'] = $vbill_config->sxfpublic; //
                $data['orgId'] = $vbill_config->orgId; //
                $data['out_trade_no'] = $order->out_trade_no;
                Log::channel('vbill')->info(date('Y-m-d H:i:s')." vbill-query-req-".$order->out_trade_no."： ", $data);
                $return = $obj->order_query($data);
                Log::channel('vbill')->info(date('Y-m-d H:i:s')." vbill-query-res-".$order->out_trade_no."： ", $return);
//                Log::info('随行付支付查询');
//                Log::info($return);

                $message = $return['message'];

                //支付成功
                if ($return["status"] == 1) {
                    $pay_time = date('Y-m-d H:i:s', strtotime($return['data']['respData']['tranTime']));
                    $trade_no = $return['data']['respData']['sxfUuid'];
                    $buyer_pay_amount = $return['data']['respData']['totalOffstAmt'];
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                    $promotionDetail = isset($return['data']['respData']['promotionDetail']) ? $return['data']['respData']['promotionDetail'] : ''; //优惠信息（jsonArray格式字符串）
                    $promotion_name = '';
                    $promotion_amount = 0.00;
                    if ($promotionDetail) {
                        $promotion_detail_arr = json_decode($promotionDetail, true);
                        if ($promotion_detail_arr && is_array($promotion_detail_arr)) {
//                    $promotion_name = isset($promotion_detail_arr[0]['name']) ? $promotion_detail_arr[0]['name'] : ''; //优惠名称
//                    $promotion_amount = isset($promotion_detail_arr[0]['amount']) ? $promotion_detail_arr[0]['amount']: 0.00; //优惠总额；单位元，保留两位小数
                            $promotion_name = '刷脸支付天天有优惠';
                            $promotion_amount = array_sum(array_column($promotion_detail_arr, 'amount'));
                        }
                    }

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);
                        $fee_amount = ($buyer_pay_amount*$order->rate)/100;
                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                            'discount_amount' => $promotion_amount, //第三方平台优惠金额
                            'fee_amount' => $fee_amount
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '13000',//返佣来源
                            'source_desc' => '随行付',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : ""
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no,
                            'promotion_name' => $promotion_name,
                            'promotion_amount' => $promotion_amount
                        ]
                    ]);
                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => ($message == '成功') ? '等待支付' : $message,
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => ($message == '成功') ? '订单支付失败' : $message,
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ],
                    ]);
                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ],
                    ]);
                } else {
                    //其他情况
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ],
                    ]);
                }
            }

            //随行付a收银支付
            if (18999 < $ways_type && $ways_type < 19999) {
                //读取配置
                $config = new VbillConfigController();
                $vbill_config = $config->vbilla_config($config_id);
                if (!$vbill_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '随行付a配置不存在请检查配置'
                    ]);
                }

                $vbill_merchant = $config->vbilla_merchant($store_id, $store_pid);
                if (!$vbill_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '随行付a商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Vbill\PayController();
                $data['notify_url'] = url('/api/vbill/pay_notify_a_url'); //回调地址
                $data['request_url'] = $obj->order_query_url; //请求地址;
                $data['mno'] = $vbill_merchant->mno;
                $data['privateKey'] = $vbill_config->privateKey; //
                $data['sxfpublic'] = $vbill_config->sxfpublic; //
                $data['orgId'] = $vbill_config->orgId; //
                $data['out_trade_no'] = $order->out_trade_no;
                $return = $obj->order_query($data);
//                Log::info('随行付a支付查询');
//                Log::info($return);

                $message = $return['message'];

                //支付成功
                if ($return["status"] == 1) {
                    $pay_time = date('Y-m-d H:i:s', strtotime($return['data']['respData']['tranTime']));
                    $trade_no = $return['data']['respData']['sxfUuid'];
                    $buyer_pay_amount = $return['data']['respData']['totalOffstAmt'];
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                    $promotionDetail = isset($return['data']['respData']['promotionDetail']) ? $return['data']['respData']['promotionDetail'] : ''; //优惠信息（jsonArray格式字符串）
                    $promotion_name = '';
                    $promotion_amount = 0.00;
                    if ($promotionDetail) {
                        $promotion_detail_arr = json_decode($promotionDetail, true);
                        if ($promotion_detail_arr && is_array($promotion_detail_arr)) {
//                    $promotion_name = isset($promotion_detail_arr[0]['name']) ? $promotion_detail_arr[0]['name'] : ''; //优惠名称
//                    $promotion_amount = isset($promotion_detail_arr[0]['amount']) ? $promotion_detail_arr[0]['amount']: 0.00; //优惠总额；单位元，保留两位小数
                            $promotion_name = '刷脸支付天天有优惠';
                            $promotion_amount = array_sum(array_column($promotion_detail_arr, 'amount'));
                        }
                    }

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);
                        $fee_amount = ($buyer_pay_amount*$order->rate)/100;

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                            'discount_amount' => $promotion_amount, //第三方平台优惠金额
                            'fee_amount' => $fee_amount
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '19000',//返佣来源
                            'source_desc' => '随行付A',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : ""
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no,
                            'promotion_name' => $promotion_name,
                            'promotion_amount' => $promotion_amount
                        ]
                    ]);
                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => ($message == '成功') ? '等待支付' : $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => ($message == '成功') ? '订单支付失败' : $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ],
                    ]);
                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ],
                    ]);
                } else {
                    //其他情况
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ],
                    ]);
                }
            }

            //哆啦宝
            if (14999 < $ways_type && $ways_type < 15999) {
                $manager = new ManageController();
                //读取配置
                $dlbconfig = $manager->pay_config($config_id);
                if (!$dlbconfig) {
                    return json_encode([
                        'status' => 2,
                        'message' => '哆啦宝配置配置不存在请检查配置'
                    ]);
                }

                $dlb_merchant = $manager->dlb_merchant($store_id, $store_pid);
                if (!$dlb_merchant && !empty($dlb_merchant->mch_num) && !empty($dlb_merchant->shop_num)) {
                    return json_encode([
                        'status' => 2,
                        'message' => '哆啦宝配置商户未补充商户编号等信息!'
                    ]);
                }

                $query_data = [
                    'agentNum' => $dlbconfig->agent_num,
                    'customerNum' => $dlb_merchant->mch_num,
                    'shopNum' => $dlb_merchant->shop_num,
                    'requestNum' => $order->out_trade_no,
                    'secretKey' => $dlbconfig->secret_key,
                    'accessKey' => $dlbconfig->access_key,
                ];
                $return = $manager->query_bill($query_data);
                if ($return['status'] == 0) {
                    return json_encode($return);
                }
                if ($return['status'] == 1) {
                    $query_result = $return['data'];
                    if ($query_result['status'] == "SUCCESS") {
                        $pay_time = $query_result['completeTime'];
                        $trade_no = $query_result['orderNum'];
                        $buyer_pay_amount = $query_result['orderAmount'];
                        $buyer_id = isset($query_result['openId']) ? $query_result['openId'] : "";

                        //改变数据库状态
                        if ($order->pay_status != 1) {
                            Cache::forget($c_o);

                            $insert_data = [
                                'status' => '1',
                                'pay_status' => 1,
                                'pay_status_desc' => '支付成功',
                                'buyer_id' => $buyer_id,
                                'trade_no' => $trade_no,
                                'pay_time' => $pay_time,
                                'buyer_pay_amount' => $buyer_pay_amount
                            ];
                            $this->update_day_order($insert_data, $out_trade_no, $table);

                            //支付成功后的动作
                            $data = [
                                'ways_type' => $order->ways_type,
                                'ways_type_desc' => $order->ways_type_desc,
                                'source_type' => '15000',//返佣来源
                                'source_desc' => '哆啦宝',//返佣来源说明
                                'company' => 'dlb',//返佣来源说明
                                'total_amount' => $order->total_amount,
                                'out_trade_no' => $order->out_trade_no,
                                'rate' => $order->rate,
                                'merchant_id' => $order->merchant_id,
                                'store_id' => $order->store_id,
                                'user_id' => $order->user_id,
                                'config_id' => $config_id,
                                'store_name' => $order->store_name,
                                'ways_source' => $order->ways_source,
                                'pay_time' => $pay_time,
                                'device_id' => isset($order->device_id) ? $order->device_id : "",
                            ];
                            PaySuccessAction::action($data);
                        }

                        return json_encode([
                            'status' => 1,
                            'pay_status' => '1',
                            'message' => '支付成功',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'pay_time' => $pay_time,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $buyer_pay_amount,
                                'total_amount' => $order->total_amount,
                                'other_no' => $other_no,
                                'trade_no' => $trade_no
                            ]
                        ]);
                    } elseif ($query_result['status'] == "INIT") {
                        return json_encode([
                            'status' => 1,
                            'pay_status' => '2',
                            'message' => '等待支付',
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->total_amount,
                                'total_amount' => $order->total_amount,
                            ]
                        ]);
                    } elseif ($query_result['status'] == "CANCEL") {
                        return json_encode([
                            'status' => 1,
                            'pay_status' => '3',
                            'message' => '订单支付失败',
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount,
                            ],
                        ]);
                    } elseif ($query_result['status'] == "REFUND") {
                        return json_encode([
                            'status' => 1,
                            'pay_status' => '3',
                            'message' => '订单已经退款',
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount,
                            ],
                        ]);
                    } elseif ($query_result['status'] == "REFUNDING") {
                        return json_encode([
                            'status' => 1,
                            'pay_status' => '3',
                            'message' => '订单退款中',
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount,
                            ],
                        ]);
                    } elseif ($query_result['status'] == "FUNDFAIL") {
                        return json_encode([
                            'status' => 1,
                            'pay_status' => '3',
                            'message' => '订单退款失败',
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                }
            }

            //传化收银支付
            if (11999 < $ways_type && $ways_type < 12999) {
                //读取配置
                $config = new TfConfigController();

                $h_merchant = $config->tf_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '传化商户号不存在'
                    ]);
                }

                $h_config = $config->tf_config($config_id, $h_merchant->qd);
                if (!$h_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '传化配置不存在请检查配置'
                    ]);
                }

                $obj = new \App\Api\Controllers\Tfpay\PayController();
                $data['mch_id'] = $h_config->mch_id; //
                $data['pub_key'] = $h_config->pub_key; //
                $data['pri_key'] = $h_config->pri_key; //
                $data['sub_mch_id'] = $h_merchant->sub_mch_id; //
                $data['out_trade_no'] = $out_trade_no; //
                $data['date'] = date('Y-m-d', strtotime($created_at)); //

                $return = $obj->order_query($data);

                //支付成功
                if ($return["status"] == 1) {
                    $pay_time = $return['data']['paid_at'];
                    $trade_no = $return['data']['channel_no'];
                    $buyer_pay_amount = $return['data']['total_fee'];
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '12000',//返佣来源
                            'source_desc' => 'TF',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : ""
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no
                        ]
                    ]);
                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }

            //汇付支付
            if (17999 < $ways_type && $ways_type < 18999) {
                //交易结果查询
                $config = new HuiPayConfigController();
                $hui_pay_config = $config->hui_pay_config($config_id);
                if (!$hui_pay_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '汇付配置不存在请检查配置'
                    ]);
                }

                $hui_pay_merchant = $config->hui_pay_merchant($store_id, $store_pid);
                if (!$hui_pay_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '汇付商户不存在'
                    ]);
                }
                $hui_pay_store = $hui_pay_merchant->mer_id;
                if (!$hui_pay_store) {
                    return json_encode([
                        'status' => 2,
                        'message' => '汇付商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\HuiPay\PayController();
                $data['request_url'] = $obj->order_info; //请求地址;
                $data['mer_cust_id'] = $hui_pay_config->mer_cust_id; //商户客户号
                $data['cust_id'] = $hui_pay_merchant->user_cust_id; //用户客户号
                $data['private_key'] = $hui_pay_config->private_key;
                $data['public_key'] = $hui_pay_config->public_key;
                $data['org_id'] = $hui_pay_config->org_id;
                $data['out_trade_no'] = $order->out_trade_no; //系统订单号
                $data['device_id'] = $hui_pay_merchant->device_id??''; //机具id
                $return = $obj->order_query($data); //0-系统错误 1-成功 2-处理中 3-失败 4.初始
//                Log::info('汇付交易信息查询返回111: '.str_replace("\\/", "/", json_encode($return, JSON_UNESCAPED_UNICODE)));
                //支付成功
                if ($return["status"] == 1) {
                    if (isset($return['data']) && !empty($return['data'])) {
                        if (isset($return['data']['trans_date']) && !empty($return['data']['trans_date'])) {
                            $trans_date = date('Y-m-d', strtotime($return['data']['trans_date']));
                            $trans_time = date('H:i:s', strtotime($return['data']['trans_time']));
                            $pay_time = $trans_date.' '.$trans_time;
                        }
                        $trade_no = $return['data']['party_order_id']; //第三方支付凭证号
                        $buyer_pay_amount = $return['data']['trans_amt']; //交易金额
                        $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                        //改变数据库状态
                        if ($order->pay_status != 1) {
                            Cache::forget($c_o);
                            $insert_data = [
                                'status' => '1',
                                'pay_status' => 1,
                                'pay_status_desc' => '支付成功',
                                'buyer_logon_id' => '',
                                'trade_no' => $trade_no,
                                'buyer_pay_amount' => $buyer_pay_amount,
                            ];
                            if (isset($return['data']['trans_date']) && !empty($return['data']['trans_date'])) {
                                $insert_data['pay_time'] = $pay_time;
                            }

                            $this->update_day_order($insert_data, $out_trade_no,$table);

                            //支付成功后的动作
                            $data = [
                                'ways_type' => $order->ways_type,
                                'company' => $order->company,
                                'ways_type_desc' => $order->ways_type_desc,
                                'source_type' => '18000',//返佣来源
                                'source_desc' => '汇付',//返佣来源说明
                                'total_amount' => $order->total_amount,
                                'out_trade_no' => $order->out_trade_no,
                                'rate' => $order->rate,
                                'fee_amount' => $order->fee_amount,
                                'merchant_id' => $order->merchant_id,
                                'store_id' => $order->store_id,
                                'user_id' => $order->user_id,
                                'config_id' => $config_id,
                                'store_name' => $order->store_name,
                                'ways_source' => $order->ways_source,
                                'device_id' => isset($order->device_id) ? $order->device_id : "",
                            ];
                            if (isset($return['data']['trans_date']) && !empty($return['data']['trans_date'])) {
                                $data['pay_time'] = $pay_time;
                            }
                            PaySuccessAction::action($data);
                        }
                        return json_encode([
                            'status' => 1,
                            'pay_status' => '1',
                            'message' => '支付成功',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'pay_time' => isset($return['data']['trans_date'])?$pay_time:'',
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $buyer_pay_amount,
                                'total_amount' => $order->total_amount,
                                'other_no' => $other_no,
                                'trade_no' => $trade_no,
                            ]
                        ]);
                    }
                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);

                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);

                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);
                }
            }
            //工行收银支付
            if (19999 < $ways_type && $ways_type < 20500) {
                $config = new LianfuConfigController();

                $h_merchant = $config->lianfu_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '工行商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\lianfu\PayController();
                $data['apikey'] = $h_merchant->apikey; //
                $data['signkey'] = $h_merchant->signkey; //
                $data['appid'] = $h_merchant->appid; //收单机构APPID
                $data['total_amount'] = $order->total_amount; //
                $data['out_trade_no'] = $out_trade_no; //

                $return = $obj->order_query($data);
//file_put_contents('ceshi.log',var_export($return,true)."\r\n",FILE_APPEND);
                //支付成功
                if ($return["status"] == 1) {
                    $pay_time = date('Y-m-d H:i:s', time());
                    $trade_no = isset($return['data']['order_id'])?$return['data']['order_id']:$out_trade_no;
                    $buyer_pay_amount = $order->total_amount;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '20200',//返佣来源
                            'source_desc' => 'lianfu',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : ""
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'ways_source' => $order->ways_source,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'ways_source' => $order->ways_source,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }

            //海科融通 交易查询
            if (21999 < $ways_type && $ways_type < 22999) {
                //读取配置
                $config = new HkrtConfigController();
                $hkrt_config = $config->hkrt_config($config_id);
                if (!$hkrt_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '海科融通配置不存在请检查配置'
                    ]);
                }

                $hkrt_merchant = $config->hkrt_merchant($store_id, $store_pid);
                if (!$hkrt_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '海科融通商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Hkrt\PayController();
                $hkrt_data = [
                    'access_id' => $hkrt_config->access_id,
                    'trade_no' => $order->trade_no,
                    'out_trade_no' => $order->out_trade_no,
                    'access_key' => $hkrt_config->access_key
                ];
                Log::info('海科融通-商户-交易查询');
                Log::info($hkrt_data);
                $return = $obj->order_query($hkrt_data); //0-系统错误 1-交易成功；2-交易失败；3-交易进行中；4-交易超时
                Log::info($return);

                //支付成功
                if ($return['status'] == '1') {
                    $pay_time = $return['data']['trade_end_time']; //SaaS平台交易完成时间
                    $trade_no = $return['data']['trade_no']; //SaaS平台的交易订单编号
                    $buyer_pay_amount = $return['data']['total_amount']; //订单金额,以元为单位
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '22000', //返佣来源
                            'source_desc' => '海科融通', //返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                } //交易进行中
                elseif ($return['status'] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                } //交易失败
                elseif ($return['status'] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);
                }//交易超时
                elseif ($return['status'] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '交易超时',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);
                }
            }

            //易生支付 交易查询61002
            if (in_array($ways_type,[61001,61002,61004,21001,21002,21004])) {
                //读取配置
                $version="easypay";
                if(in_array($ways_type,[61001,61002,61004])){
                    $version="easypaya";
                }
                $config = new EasyPayConfigController();
                $easypay_config = $config->easypay_config($config_id);
                if (!$easypay_config) {
                    return json_encode([
                        'status' => '2',
                        'message' => '易生支付配置不存在请检查配置'
                    ]);
                }
                $easypay_merchant = $config->easypay_merchant($store_id, $store_pid,$version);
                if (!$easypay_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '易生支付商户号不存在'
                    ]);
                }
                $obj = new \App\Api\Controllers\EasyPay\PayController();
                $easypay_data = [
                    'channel_id' => $easypay_config->channel_id,
                    'mno' => $easypay_merchant->termMercode,
                    'device_id' => $easypay_merchant->termTermcode,
                    'out_trade_no' => $order->out_trade_no,
                    'private_key' => $easypay_config->private_key
                ];
                $return = $obj->order_query($easypay_data); //-1 系统错误 0-其他 1-成功 2-下单失败 3-订单未支付
                Log::channel("easypay")->info("订单查询",[
                    "reqData"=>$easypay_data,
                    "resData"=>$return
                ]);
                //支付成功
                if ($return['status'] == '1') {
                    $trade_no = $return['data']['outTrace']; //系统订单号
                    $pay_time = (isset($return['data']['timeEnd']) && !empty($return['data']['timeEnd'])) ? date('Y-m-d H:i:m', strtotime($return['data']['timeEnd'])): ''; //支付完成时间，格式为 yyyyMMddhhmmss，如 2009年12月27日9点10分10秒表示为 20091227091010
                    $payamt = isset($return['data']['payerAmt']) ? ($return['data']['payerAmt'] / 100): ''; //实付金额，单位分
                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        \App\Models\OrderExtend::create([
                            'out_trade_no' => $out_trade_no,
                            "channel_code"=>2,
                            'transactionId' => isset($return['data']['pcTrace']) ? $return['data']['pcTrace'] : '',
                            'user_bar_code' => isset($return['data']['outTrace']) ? $return['data']['outTrace'] : '',
                        ]);
                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $payamt,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '21000', //返佣来源
                            'source_desc' => '易生支付', //返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $payamt,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                } elseif($return['status'] == '2') {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => 2,
                        'message' => '待支付',
                        'data' => [
                            'out_trade_no' =>  $order->out_trade_no,
                            'ways_type' => $order->ways_source,
                            'total_amount' => $order->total_amount,
                            'store_id' => $order->store_id,
                            'store_name' => $order->store_name,
                            'config_id' => $order->config_id,
                            'ways_source' => $order->ways_source,
                        ]
                    ]);
                } elseif ($return['status'] == '3') {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '订单未支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } //其他情况
                else {
                    return json_encode([
                        'status' => 2,
                        'pay_status' => 3,
                        'message' => $return['message'],
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);
                }
            }
            //易生支付 交易查询
            if (false) {
                //读取配置
                $config = new EasyPayConfigController();
                $easypay_config = $config->easypay_config($config_id);
                if (!$easypay_config) {
                    return json_encode([
                        'status' => '2',
                        'message' => '易生支付配置不存在请检查配置'
                    ]);
                }

                $easypay_merchant = $config->easypay_merchant($store_id, $store_pid);
                if (!$easypay_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '易生支付商户号不存在4'
                    ]);
                }

                $obj = new \App\Api\Controllers\EasyPay\PayController();
                $easypay_data = [
                    'channel_id' => $easypay_config->channel_id, //渠道编号
                    'mer_id' => $easypay_merchant->termMercode, //终端商戶编号
                    'device_id' => $easypay_merchant->termTermcode, //终端编号
                    'out_trade_no' => $order->out_trade_no //原交易流水
                ];
//                Log::info('易生支付-商户-交易查询');
//                Log::info($easypay_data);
                $return = $obj->order_query($easypay_data); //-1 系统错误 0-其他 1-成功 2-下单失败 3-订单未支付
//                Log::info($return);

                //支付成功
                if ($return['status'] == '1') {
                    $pay_time = $return['data']['wxtimeend'] ? date('Y-m-d H:i:s', strtotime($return['data']['wxtimeend'])): ''; //支付完成时间，如2009年12月27日9点10分10秒表示为20091227091010
                    $trade_no = $return['data']['wtorderid']; //系统订单号
                    $buyer_logon_id = $return['data']['wxopenid'] ?? ''; //用户在商户 appid 下的唯一标识(微信/支付宝渠道返回)
                    $buyer_pay_amount = $return['data']['payamt'] ? ($return['data']['payamt']/100): ''; //实付金额，单位分
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '21000', //返佣来源
                            'source_desc' => '易生支付', //返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                } elseif($return['status'] == '2') {
                    return json_encode([
                        'status' => '2',
                        'pay_status' => '3',
                        'message' => '支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                } elseif ($return['status'] == '3') {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '订单未支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } //其他情况
                else {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => 3,
                        'message' => $return['message'],
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);
                }
            }

            //邮储收银支付
            if (26000 < $ways_type && $ways_type < 26999) {
                $config = new LianfuyoupayConfigController();
                $h_merchant = $config->lianfu_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '邮政支付商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\lianfuyouzheng\PayController();
                $data['apikey'] = $h_merchant->apikey; //
                $data['signkey'] = $h_merchant->signkey; //
                $data['out_trade_no'] = $out_trade_no; //
                $data['pos_sn'] = $h_merchant->pos_sn; //
                $return = $obj->order_query($data);
                //file_put_contents('b423532.log',var_export($return,true)."\r\n",FILE_APPEND);

                //支付成功
                if ($return["status"] == 1) {
                    $pay_time = date('Y-m-d H:i:s', time());
                    $trade_no = isset($return['data']['orderNo'])?$return['data']['orderNo']:$out_trade_no;
                    $buyer_pay_amount = $order->total_amount;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '26000', //返佣来源
                            'source_desc' => 'lianfuyoupay', //返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : ""
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'ways_source'=>$order->ways_source,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'ways_source'=>$order->ways_source,
                            'other_no' => $other_no
                        ]
                    ]);
                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'ways_source'=>$order->ways_source,
                            'total_amount' => $order->total_amount
                        ]
                    ]);

                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'ways_source'=>$order->ways_source,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'ways_source'=>$order->ways_source,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }

            //苏州收银支付
            if (17000 < $ways_type && $ways_type < 17999) {
                //读取配置
                $config = new SuzhouConfigController();
                $h_merchant = $config->sz_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '苏州银行商户号不存在'
                    ]);
                }

                $h_config = $config->sz_config($config_id);
                if (!$h_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '苏州银行支付配置不存在请检查配置'
                    ]);
                }

                $obj = new \App\Api\Controllers\Suzhou\PayController();
                $data['certId'] = $h_config->certId; //
                $data['pri_key'] = $h_config->rsa_pr; //
                $data['merchantId'] = $h_merchant->MerchantId; //
                $data['out_trade_no'] = $out_trade_no; //
                $data['orderTime'] = date('YmdHis', time()); //
                $return = $obj->order_query($data);

                //支付成功
                if ($return["status"] == 1) {
                    $trade_no = $return['data']['tradeNo'];
                    $user_info = '';
                    $pay_time = date('Y-m-d H:i:s', strtotime($return['data']['payTime']));
                    $buyer_pay_amount = $order->total_amount;
                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            // 'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '17000',//返佣来源
                            'source_desc' => 'SZ',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : ""
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no
                        ]
                    ]);
                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);

                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }

            //葫芦天下
            if (22999 < $ways_type && $ways_type < 23999) {
                $manager = new \App\Api\Controllers\Hltx\ManageController();
                $hltx_merchant = $manager->pay_merchant($store_id, $store_pid);
                $qd = $hltx_merchant->qd;
                $hltx_config = $manager->pay_config($config_id, $qd);
                if (!$hltx_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => 'HL支付配置不存在请检查配置'
                    ]);
                }

                $manager->init($hltx_config);
                switch ($ways_type) {
                    case 23001:
                        $pay_channel = 'ALI';
                        break;
                    case 23002:
                        $pay_channel = 'WX';
                        break;
                    case 23004:
                        $pay_channel = 'UPAY';
                        break;
                    default:
                        $pay_channel = 'ALI';
                        break;
                }

                if (!$hltx_merchant || empty($hltx_merchant->mer_no)) {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户未成功开通HL通道!'
                    ]);
                }

                $hltx_data = [
                    'merNo' => $hltx_merchant->mer_no,
                    'orderNo' => $order->out_trade_no,
                ];
                $return = $manager->query_order($hltx_data);
                if ($return['status'] == 1) {
                    $return = $return['data'];
                    if ($return['tradeStatus'] == "S") {
                        if ($return['subTradeStatus'] == 'COMPLETE') {
                            $pay_time = $return['payCompleteTime'];
                            $trade_no = $return['transIndex'];
                            $buyer_pay_amount = $return['amount'] / 100;
                            //改变数据库状态
                            if ($order->pay_status != 1) {
                                Cache::forget($c_o);

                                $insert_data = [
                                    'status' => '1',
                                    'pay_status' => 1,
                                    'pay_status_desc' => '支付成功',
                                    'buyer_logon_id' => '',
                                    'trade_no' => $trade_no,
                                    'pay_time' => $pay_time,
                                    'buyer_pay_amount' => $buyer_pay_amount,
                                ];
                                $this->update_day_order($insert_data, $out_trade_no, $table);

                                //支付成功后的动作
                                $data = [
                                    'ways_type' => $order->ways_type,
                                    'ways_type_desc' => $order->ways_type_desc,
                                    'source_type' => '23000', //返佣来源
                                    'source_desc' => 'HL', //返佣来源说明
                                    'company' => 'hltx', //返佣来源说明
                                    'total_amount' => $order->total_amount,
                                    'out_trade_no' => $order->out_trade_no,
                                    'rate' => $order->rate,
                                    'merchant_id' => $order->merchant_id,
                                    'store_id' => $order->store_id,
                                    'user_id' => $order->user_id,
                                    'config_id' => $config_id,
                                    'store_name' => $order->store_name,
                                    'ways_source' => $order->ways_source,
                                    'pay_time' => $pay_time,
                                    'device_id' => isset($order->device_id) ? $order->device_id : ""
                                ];
                                PaySuccessAction::action($data);
                            }

                            $return_data = [
                                'status' => 1,
                                'pay_status' => '1',
                                'message' => '支付成功',
                                'data' => [
                                    'ways_source' => $order->ways_source,
                                    'pay_time' => $pay_time,
                                    'out_trade_no' => $order->out_trade_no,
                                    'pay_amount' => $buyer_pay_amount,
                                    'total_amount' => $order->total_amount,
                                    'other_no' => $other_no,
                                    'trade_no' => $trade_no
                                ]
                            ];
                        } elseif (in_array($return['subTradeStatus'], ['FAILED', 'SYS_FAILED', 'CANCEL', 'CLOSE'])) {
                            $return_data = [
                                'status' => 1,
                                'pay_status' => '3',
                                'message' => '订单支付失败',
                                'data' => [
                                    'out_trade_no' => $order->out_trade_no,
                                    'pay_amount' => $order->pay_amount,
                                    'total_amount' => $order->total_amount
                                ],
                            ];
                        } elseif ($return['subTradeStatus'] == 'PAYING') {
                            $return_data = [
                                'status' => 1,
                                'pay_status' => '2',
                                'message' => '等待支付',
                                'data' => [
                                    'out_trade_no' => $order->out_trade_no,
                                    'pay_amount' => $order->total_amount,
                                    'total_amount' => $order->total_amount
                                ]
                            ];
                        }
                    } elseif ($return['tradeStatus'] == "E") {
                        $return_data = [
                            'status' => 1,
                            'pay_status' => '3',
                            'message' => '订单支付失败',
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ],
                        ];
                    } else {
                        $return_data = [
                            'status' => 1,
                            'pay_status' => '2',
                            'message' => '等待支付',
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->total_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ];
                    }
                } else {
                    //其他情况
                    $message = $return['message'];
                    $return_data = [
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ];
                }

                return json_encode($return_data);
            }

            //长沙收银支付
            if (25000 < $ways_type && $ways_type < 25999) {
                $config = new ChangshaConfigController();
                $h_merchant = $config->cs_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '长沙银行商户号不存在'
                    ]);
                }

                $h_config = $config->cs_config($data['config_id']);
                if (!$h_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '长沙银行支付配置不存在请检查配置'
                    ]);
                }

                $obj = new \App\Api\Controllers\Changsha\PayController();
                $data['ECustId'] = $h_merchant->ECustId; //
                $data['out_trade_no'] = $out_trade_no; //
                $return = $obj->order_query($data);

                //支付成功
                if ($return["status"] == 1) {
                    $trade_no = $return['data']['OrderId'];
                    $user_info = '';
                    $pay_time = date('Y-m-d H:i:s', time());
                    $buyer_pay_amount = $order->total_amount;

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            // 'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '25000',//返佣来源
                            'source_desc' => 'changsha',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : ""
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no
                        ]
                    ]);
                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                }
            }

            //联动优势 交易查询
            if (4999 < $ways_type && $ways_type < 5999) {
                $config = new LinkageConfigController();

                $obj = new \App\Api\Controllers\Linkage\PayController();
                $linkage_data = [
                    'out_trade_no' => $out_trade_no //商户订单号
                ];
                $result = $obj->order_query($linkage_data);

                //支付成功
                if ($result['pay_status'] == '1') {
                    
                    $pay_time = date('Y-m-d H:i:s', time()); //交易时间 格式：MMDDhhmmss
                    
                    $trade_no = $result['data']['trade_no']; //联动优势的流水号
                    
                    $buyer_pay_amount = $result['data']['total_amount'] ? ($result['data']['total_amount']/100): ''; //订单金额 (打印小票使用)
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                    $paySeq = ''; //支付流水号（条形码），成功返回
                    $depBankSeq = $result['data']['out_trade_no'] ?? ''; //第三方流水号（微信/支付宝/银联的交易流水号）
                    // print_r(2);die;
                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                            'auth_code' => $paySeq,
                            'other_no' => $depBankSeq
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '5000', //返佣来源
                            'source_desc' => '联动优势', //返佣来源说明
                            'total_amount' => $buyer_pay_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                }else if($result['pay_status'] == '2') {
                    // print_r(2);die;
                    return json_encode([
                        'status' => '1',
                        'pay_status' => '2',
                        'message' => '支付中',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
                else {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => 3,
                        'message' => $result['message'],
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }

            //威富通 交易查询
            if (26999 < $ways_type && $ways_type < 27999) {
                $config = new WftPayConfigController();
                $version="v1";
                if(in_array($ways_type,[27101, 27102, 27103, 27104, 27105, 27107])){
                    $version="v2";
                }
                $wftpay_config = $config->wftpay_config($config_id,$version);
                if (!$wftpay_config) {
                    return json_encode([
                        'status' => '2',
                        'message' => '威富通支付配置不存在请检查配置'
                    ]);
                }

                $wftpay_merchant = $config->wftpay_merchant($store_id, $store_pid,$version);
                if (!$wftpay_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '威富通商户号不存在'
                    ]);
                }

                //支付状态为 退款中、已退款、有退款，退款查询接口
                $obj = new WftPayPayController();
                $wftpay_data = [
                    'mch_id' => $wftpay_merchant->mch_id,
                    'out_trade_no' => $out_trade_no,
                    'private_rsa_key' => $wftpay_config->private_rsa_key,
                    'public_rsa_key' => $wftpay_config->public_rsa_key
                ];
                if ($order->pay_status == 5 || $order->pay_status == 6 || $order->pay_status == 7) {
                    $return = $obj->refund_query($wftpay_data); //0-系统错误 1-成功 2-退款失败 3-退款处理中 4-退款到银行发现用户的卡作废或者冻结了,导致原路退款银行卡失败
                    Log::info('威富通-退款查询');
                    Log::info($return);

                    //支付成功
                    if ($return['status'] == 1) {
                        $refund_count = $return['data']['refund_count'] -1; //退款记录数
                        $refund_no = $return['data']['out_refund_id_'.$refund_count] ?? $return['data']['out_refund_no_'.$refund_count]; //第三方退款单号or平台退款单号
                        $pay_time = isset($return['data']['refund_time_'.$refund_count]) ? date('Y-m-d H:i:s', strtotime($return['data']['refund_time_'.$refund_count])): ''; //退款时间,yyyyMMddHHmmss
                        $refund_amount = isset($return['data']['refund_fee_'.$refund_count]) ? ($return['data']['refund_fee_'.$refund_count]/100): $order->total_amount; //退款总金额,单位为分,可以做部分退款
                        $refund_amount = number_format($refund_amount, 2, '.', '');
                        $trade_no = $return['data']['out_transaction_id']; //第三方订单号

                        //改变数据库状态
                        if ($order->pay_status != 6) {
                            Cache::forget($c_o);
                            $insert_data = [
                                'status' => '6',
                                'pay_status' => 6,
                                'pay_status_desc' => '退款成功',
                                'refund_no' => $refund_no,
                                'refund_amount' => $refund_amount
                            ];
                            try {
                                if ($pay_time) $insert_data['pay_time'] = $pay_time;
                                $this->update_day_order($insert_data, $out_trade_no, $table);
                                //退款成功后的动作
                                $return_data = [
                                    'out_trade_no' => $out_trade_no,
                                    'trade_no' => $trade_no,
                                    'store_id' => $order->store_id,
                                    'merchant_id' => $order->merchant_id,
                                    'type' => $order->ways_type,
                                    'ways_source' => $order->ways_source,
                                    'refund_amount' => $refund_amount,
                                    'refund_no' => $refund_no
                                ];
                                RefundOrder::query()->create($return_data);
                                RefundSuccessAction::action($return_data);
                            }catch (\Exception $e){
                                $insert_data["error"]="test";
                                Log::info("wftpay-退款异常",[
                                    "error"=>$e->getMessage(),
                                    "data"=>$insert_data
                                ]);
                            }
                        }

                        return json_encode([
                            'status' => '1',
                            'pay_status' => 6,
                            'message' => '退款成功',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'pay_time' => $pay_time,
                                'out_trade_no' => $order->out_trade_no,
                                'refund_amount' => $refund_amount,
                                'total_amount' => $order->total_amount,
                                'other_no' => $other_no,
                                'trade_no' => $trade_no
                            ]
                        ]);
                    }
                    elseif($return['status'] == 2) {
                        return json_encode([
                            'status' => '2',
                            'pay_status' => '2',
                            'message' => '支付中',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                    else {
                        return json_encode([
                            'status' => '1',
                            'pay_status' => 3,
                            'message' => $return['message'],
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                } else {
                    Log::channel("wftpay")->info("威富通-交易查询",$wftpay_data);
                    $return = $obj->order_query($wftpay_data); //0-系统错误 1-成功 2-失败 3-转入退款 4-未支付 5-已关闭 6-已冲正 7-已撤销 8-用户支付中
                    Log::channel("wftpay")->info("威富通-交易查询结果",$return);
                    //支付成功
                    if ($return['status'] == 1) {
                        $pay_time = $return['data']['time_end'] ? date('Y-m-d H:i:s', strtotime($return['data']['time_end'])): ''; //支付完成时间，格式为yyyyMMddHHmmss，如2009年12月27日9点10分10秒表示为20091227091010
                        $trade_no = $return['data']['transaction_id']; //平台交易号
                        $buyer_pay_amount = $return['data']['total_fee'] ? ($return['data']['total_fee']/100): ''; //总金额，以分为单位，不允许包含任何字、符号
                        $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                        $depBankSeq = $return['data']['out_transaction_id'] ?? ''; //第三方交易号

                        $buyer_user_id="";
                        if(!empty($return['data']['buyer_user_id'])){
                            $buyer_user_id = $return['data']['buyer_user_id'] ?? ''; //	买家在支付宝的用户id
                        }

                        if(!empty($return['data']['sub_openid'])){
                            $buyer_user_id = $return['data']['sub_openid'] ?? ''; //	买家在支付宝的用户id
                        }


                        //改变数据库状态
                        if ($order->pay_status != 1) {
                            Cache::forget($c_o);

                            $insert_data = [
                                'status' => '1',
                                'pay_status' => 1,
                                'pay_status_desc' => '支付成功',
                                'trade_no' => $trade_no,
                                'pay_time' => $pay_time,
                                'buyer_pay_amount' => $buyer_pay_amount,
                                'buyer_id' => $buyer_user_id,
                                'buyer_logon_id' => $buyer_user_id
                                // 'other_no' => $depBankSeq
                            ];
                            $this->update_day_order($insert_data, $out_trade_no, $table);

                            //支付成功后的动作
                            $data = [
                                'ways_type' => $order->ways_type,
                                'company' => $order->company,
                                'ways_type_desc' => $order->ways_type_desc,
                                'source_type' => '27000', //返佣来源
                                'source_desc' => '威富通', //返佣来源说明
                                'total_amount' => $buyer_pay_amount,
                                'out_trade_no' => $order->out_trade_no,
                                'rate' => $order->rate,
                                'fee_amount' => $order->fee_amount,
                                'merchant_id' => $order->merchant_id,
                                'store_id' => $order->store_id,
                                'user_id' => $order->user_id,
                                'config_id' => $config_id,
                                'store_name' => $order->store_name,
                                'ways_source' => $order->ways_source,
                                'pay_time' => $pay_time,
                                'device_id' => isset($order->device_id) ? $order->device_id : "",
                            ];
                            PaySuccessAction::action($data);
                        }

                        return json_encode([
                            'status' => '1',
                            'pay_status' => 1,
                            'message' => '支付成功',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'pay_time' => $pay_time,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $buyer_pay_amount,
                                'total_amount' => $order->total_amount,
                                'other_no' => $other_no,
                                'trade_no' => $trade_no
                            ]
                        ]);
                    }
                    elseif($return['status'] == 2) {
                        return json_encode([
                            'status' => '2',
                            'pay_status' => '3',
                            'message' => '支付失败',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                    elseif($return['status'] == 8) {
                        return json_encode([
                            'status' => '1',
                            'pay_status' => '2', //1-成功 2-支付中
                            'message' => '用户支付中',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                    else {
                        return json_encode([
                            'status' => '1',
                            'pay_status' => 3,
                            'message' => $return['message'],
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                }

            }

            //扫呗 交易查询
            if (30080 == $ways_type || 30081 == $ways_type || 30082 == $ways_type) {
                $config = new SaobeiConfigController();

                $h_merchant = $config->saobei_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '扫呗商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\lianfu\PayController();
                $wftpay_data = [
                    'pay_ver' => '100',
                    'pay_type' => '000',
                    'service_id' => '020',
                    'merchant_no' => $h_merchant->merchantNo,
                    'terminal_id' => $h_merchant->zhongduan,
                    'terminal_trace' => $out_trade_no,
                    'terminal_time'=>date('YmdHis', time()),
                    'out_trade_no'=>$order->trade_no
                ];
                $return = $obj->order_query3($wftpay_data,$h_merchant->access_token);
                //支付成功
                if ($return['status'] == 1) {
                    $pay_time = date('Y-m-d H:i:s', time());
                    $trade_no = $return['data']['out_trade_no'];
                    $buyer_pay_amount = $order->total_amount;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                    $depBankSeq = $return['data']['out_trade_no'];

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                            'other_no' => $depBankSeq
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '30080', //返佣来源
                            'source_desc' => '扫呗', //返佣来源说明
                            'total_amount' => $buyer_pay_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => 1,
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                }
                elseif($return['status'] == 2) {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => '2', //1-成功 2-支付中
                        'message' => '用户支付中',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
                else {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => 3,
                        'message' => $return['message'],
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }

            }
            
            //建行 交易查询
            if (36001 == $ways_type || 36002 == $ways_type || 36003 == $ways_type) {
                $config = new CcbConfigController();

                $h_merchant = $config->ccb_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '建行商户号不存在'
                    ]);
                }
                if($ways_type==36001){
                    $typeorder=2;
                }else if($ways_type==36002){
                    $typeorder=3;
                }else if($ways_type==36003){
                    $typeorder=4;
                }
                $obj = new \App\Api\Controllers\lianfu\PayController();
                $data1['MERCHANTID'] = $h_merchant->merchantid;//商户号
                $data1['POSID'] = $h_merchant->posid;//柜台号
                $data1['BRANCHID'] = $h_merchant->bankchid;//分行号
                $data1['TXCODE'] = 'PAY101';//交易码
                $data1['MERFLAG'] = 1;//商户类型
                $data1['ORDERID'] = $order->out_trade_no;//商户订单号
                $data1['QRYTIME'] = 1;//交易金额
                $data1['QRCODETYPE'] = $typeorder;//条码信息
                $return = $obj->order_query_ccb($data1,$h_merchant->pubkey);
                //支付成功
                if ($return['status'] == 1) {
                    $pay_time = date('Y-m-d H:i:s', time());
                    $trade_no = $return['data']['ORDERID'];
                    $buyer_pay_amount = $order->total_amount;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                    $depBankSeq = $return['data']['ORDERID'];
                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                            'other_no' => $depBankSeq
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '36001', //返佣来源
                            'source_desc' => '建行', //返佣来源说明
                            'total_amount' => $buyer_pay_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => 1,
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                }
                elseif($return['status'] == 2) {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => '2', //1-成功 2-支付中
                        'message' => '用户支付中',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                            'ways_source'=>$order->ways_source
                        ]
                    ]);
                }
                else {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => 3,
                        'message' => '支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                            'ways_source'=>$order->ways_source
                        ]
                    ]);
                }

            }

            //酷点 交易查询
            if (30090 == $ways_type || 30091 == $ways_type || 30092 == $ways_type) {
                $config = new CoolConfigController();

                $h_merchant = $config->cool_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '酷点商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\lianfu\PayController();
                $karray=array(
                    'mch_id'=>$h_merchant->merchantNo,
                    'timestamp'=>time(),
                    'nonce_str'=>md5(mt_rand(1000, 9999).time()),
                    'sign_type'=>'MD5',
                    'content'=>json_encode(array('out_trade_no'=>$out_trade_no))
                );
                foreach (array_filter($karray) as $k => $v)
                {
                    $Parameters[$k] = $v;
                }
                $buff = "";
                ksort($Parameters);
                foreach ($Parameters as $k => $v) {
                    $buff .= $k . "=" . $v . "&";
                }
                $String = '';
                if (strlen($buff) > 0) {
                    $String = substr($buff, 0, strlen($buff) - 1);
                }
                $String = $String . "&secret_key=" . $h_merchant->secret_key;
                $String = strtoupper(md5($String));
                $karray['sign'] = $String;
                $return = $obj->order_query4($karray);
                //支付成功
                if ($return['status'] == 1) {
                    $pay_time = date('Y-m-d H:i:s', time());
                    $trade_no = $return['data']['pay_no'];
                    $buyer_pay_amount = $order->total_amount;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                    $depBankSeq = $return['data']['pay_no'];

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                            'other_no' => $depBankSeq
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '30080', //返佣来源
                            'source_desc' => '扫呗', //返佣来源说明
                            'total_amount' => $buyer_pay_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => 1,
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                }
                elseif($return['status'] == 2) {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => '2', //1-成功 2-支付中
                        'message' => '用户支付中',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
                else {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => 3,
                        'message' => $return['message'],
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }

            }

            //银商 交易查询
            //if (30020 == $ways_type || 30021 == $ways_type || 30022 == $ways_type) {
            if(in_array($ways_type,[30020,30021,30022,30023,30024,30025])){
                $config = new MerchConfigController();

                $version="v1";
                if(in_array($ways_type,[30023,30024,30025])){
                    $version="v2";
                }

                $h_merchant = $config->merch_merchant($store_id, $store_pid,$version);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '银联商务商户号不存在'
                    ]);
                }

                $obj = new WftPayPayController();
                $wftpay_data = [
                    'merchantCode' => $h_merchant->MerchantId,
                    'merchantOrderId' => $out_trade_no,
                    'terminalCode' => $h_merchant->merchzhongduanid,
                ];
                $Authorization=$h_merchant->accessToken;
                if(in_array($ways_type,[30020,30021,30022])){
                    $return = $obj->order_query1($Authorization,$wftpay_data);
                }elseif(in_array($ways_type,[30023,30024,30025])){

                    $wftpay_data["appId"] = $h_merchant->merch_appid;
                    $wftpay_data["appKey"] = $h_merchant->merch_appkey;
                    $wftpay_data["store_id"] = $store_id;
                    $return = $obj->order_queryV6($wftpay_data);
                }
                //支付成功
                if ($return['status'] == 1) {
                    $pay_time = date('Y-m-d H:i:s', time());
                    $trade_no = $return['data']['out_transaction_id'];
                    $buyer_pay_amount = $order->total_amount;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                    $depBankSeq = $return['data']['out_transaction_id'];

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                            'other_no' => $depBankSeq
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '27000', //返佣来源
                            'source_desc' => '威富通', //返佣来源说明
                            'total_amount' => $buyer_pay_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => 1,
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                }
                elseif($return['status'] == 2) {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => '2', //1-成功 2-支付中
                        'message' => '用户支付中',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                            'ways_source' => $order->ways_source,
                        ]
                    ]);
                }
                else {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => 3,
                        'message' => $return['message'],
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }

            }

            //平安银行 交易查询
            if (30011 == $ways_type || 30012 == $ways_type || 30013 == $ways_type) {
                $config = new PinganConfigController();
                $h_merchant = $config->pingan_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '平安银行商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\lianfu\PayController();
                if(!empty($order->trade_no)){
                    $pingan_data['third_order_id']=$order->trade_no;
                }else{
                    $pingan_data['out_order_id']=$out_trade_no;
                }
                $pingan_data['enterpriseReg']=$h_merchant->enterpriseReg;
                $pingan_data['merchant_id']=$h_merchant->merchant_id;

                foreach (array_filter($pingan_data) as $k => $v){
                    $Parameters[$k] = $v;
                }
                $buff = "";
                ksort($Parameters);
                foreach ($Parameters as $k => $v) {
                    $buff .= $k . "=" . $v . "&";
                }
                $String = '';
                if (strlen($buff) > 0) {
                    $String = substr($buff, 0, strlen($buff) - 1);
                }
                $String = $String . $h_merchant->appsecret;
                $re=hash('sha256', $String, true);
                $String = strtoupper(bin2hex($re));
                $json='{"appid":"'."$h_merchant->appid".'"'.',"timeStamp":"'.time().'000'.'"'.',"data":'.stripslashes(json_encode($pingan_data,JSON_UNESCAPED_SLASHES)).',"sign":"'.$String.'"'.'}';
                file_put_contents('pa1.log',var_export($json,true)."\r\n",FILE_APPEND);
                $return = $obj->order_query6($json);
                file_put_contents('pa2.log',var_export($return,true)."\r\n",FILE_APPEND);
                //支付成功
                if ($return['status'] == 1) {
                    $pay_time = date('Y-m-d H:i:s', time());
                    $trade_no = $return['data']['third_order_id'];
                    $buyer_pay_amount = $order->total_amount;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                    $depBankSeq = $return['data']['leshua_order_id'];

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'qwx_no'=>$depBankSeq,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '30011', //返佣来源
                            'source_desc' => '平安银行', //返佣来源说明
                            'total_amount' => $buyer_pay_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => 1,
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $depBankSeq
                        ]
                    ]);
                }
                elseif($return['status'] == 2) {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => '2', //1-成功 2-支付中
                        'message' => '用户支付中',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
                elseif($return['status'] == 4) {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => '2', //1-成功 2-支付中
                        'message' => '用户支付中',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
                else {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => 3,
                        'message' => '支付错误',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }

            }

            //农行bmp 交易查询
            if (30001 == $ways_type || 30002 == $ways_type || 30003 == $ways_type) {
                $config = new AbcConfigController();

                $h_merchant = $config->abc_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '农行商户号不存在'
                    ]);
                }

                $obj = new WftpayPayController();
                $data1['trans_type'] = '02';//交易类型
                $data1['mer_id'] = $h_merchant->MerchantId;//key
                $data1['term_id'] = $h_merchant->abczhongduanid;//终端号
                $data1['out_trade_no'] = $out_trade_no;
                $data1['total_amount'] = $order->total_amount;
                $data1['app_id'] = $h_merchant->abckey;//商户号
                $data1['private_rsa_key'] = $h_merchant->private_rsa_key;
                $data1['public_rsa_key'] = $h_merchant->public_rsa_key;
                $return = $obj->order_query2($data1);
                //支付成功
                if ($return['status'] == 1) {
                    $pay_time = date('Y-m-d H:i:s', time());
                    $trade_no = $return['data']['order_id'];
                    $buyer_pay_amount = $order->total_amount;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                    $depBankSeq = $return['data']['order_id'];

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'qwx_no'=>$return['data']['order_id'],
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                            'other_no' => $depBankSeq
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '30011', //返佣来源
                            'source_desc' => '平安银行', //返佣来源说明
                            'total_amount' => $buyer_pay_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => 1,
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                }
                elseif($return['status'] == 2) {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => '2', //1-成功 2-支付中
                        'message' => '用户支付中',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
                else {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => 3,
                        'message' => $return['message'],
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }

            }

            //翼支付 交易查询
            if (30050 == $ways_type || 30051 == $ways_type || 30052 == $ways_type || 30053 == $ways_type) {
                $config = new WingConfigController();
                $h_merchant = $config->wing_merchant($store_id, $store_pid);
                if(!$h_merchant){
                    $h_merchant=$config->wing_merchant($order->store_id,0);
                }
                if (!$h_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '翼支付商户号不存在'
                    ]);
                }
                Log::channel("bug")->info("winpay",[
                    'data'  => $data,
                    "where" => $where,
                    "order" => $order,
                    'ctime' => date('Y-m-d H:i:s')
                ]);
                //发起查询
                $order1 = Order::where($where)->select('created_at')->first()->toArray();
//                $order1 = $order->where($where)->select('created_at')->first()->toArray();
                $obj = new \App\Api\Controllers\lianfu\PayController();
                $wftpay_data = [
                    'institutionCode' => $h_merchant->institutionCode,
                    'outTradeNo' => $out_trade_no,
                    'merchantNo' => $h_merchant->merchantNo,
                    'tradeDate'=>  $order1['created_at']
                ];
                $return = $obj->order_query1($wftpay_data);
                //支付成功
                if ($return['status'] == 1) {
                    $pay_time = date('Y-m-d H:i:s', time());
                    $trade_no = $return['data']['bankRequestNo'];
                    $buyer_pay_amount = $order->total_amount;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                    $depBankSeq = $return['data']['bankRequestNo'];

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                            'other_no' => $other_no
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '30050', //返佣来源
                            'source_desc' => '翼支付', //返佣来源说明
                            'total_amount' => $buyer_pay_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => 1,
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                }
                elseif($return['status'] == 2) {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => '2', //1-成功 2-支付中
                        'message' => '用户支付中',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
                else {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => 3,
                        'message' => '支付错误',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }

            }
            //农商行 交易查询
            if (20801 == $ways_type || 20802 == $ways_type || 20803 == $ways_type) {
                $config = new NongjinConfigController();
                $h_merchant = $config->nongjin_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '农商行商户号不存在'
                    ]);
                }
                //发起查询
                $obj = new \App\Api\Controllers\lianfu\PayController();
                $wftpay_data = [
                    'merchantNo' => $h_merchant->merchantNo,
                    'terminalNo' => $h_merchant->terminalNo,
                    'batchNo' => $h_merchant->batchNo,
                    'traceNo' => $h_merchant->batchNo,
                    'outTradeNo' => $out_trade_no,
                    'signkey' => $h_merchant->signkey,
                    'nonceStr'=>time()
                ];
                $return = $obj->order_query2($wftpay_data);
                //支付成功
                if ($return['status'] == 1) {
                    $pay_time = date('Y-m-d H:i:s', time());
//                    $trade_no = $return['data']['chnOrderId'];
                    $trade_no = isset($return['data']['itpOrderId'])?$return['data']['itpOrderId']:$out_trade_no;
                    $buyer_pay_amount = $order->total_amount;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                    $depBankSeq = $return['data']['chnOrderId'];

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                            'other_no' => $other_no
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '20801', //返佣来源
                            'source_desc' => '农商行', //返佣来源说明
                            'total_amount' => $buyer_pay_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => 1,
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                }
                elseif($return['status'] == 2) {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => '2', //1-成功 2-支付中
                        'message' => '用户支付中',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
                else {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => 3,
                        'message' => '支付错误',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }

            }

            //汇旺财 交易查询
            if (27999 < $ways_type && $ways_type < 28999) {
                $config = new HwcPayConfigController();
                $hwcpay_config = $config->hwcpay_config($config_id);
                if (!$hwcpay_config) {
                    return json_encode([
                        'status' => '2',
                        'message' => '汇旺财支付配置不存在请检查配置'
                    ]);
                }

                $hwcpay_merchant = $config->hwcpay_merchant($store_id, $store_pid);
                if (!$hwcpay_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '汇旺财商户号不存在'
                    ]);
                }

                //支付状态为 退款中、已退款、有退款，退款查询接口
                $obj = new HwcPayPayController();
                $hwcpay_data = [
                    'mch_id' => $hwcpay_merchant->mch_id,
                    'out_trade_no' => $out_trade_no,
                    'private_rsa_key' => $hwcpay_config->private_rsa_key,
                    'public_rsa_key' => $hwcpay_config->public_rsa_key
                ];
                if ($order->pay_status == 5 || $order->pay_status == 6 || $order->pay_status == 7) {
                    $return = $obj->refund_query($hwcpay_data);

                    //支付成功
                    if ($return['status'] == 1) {
                        $refund_count = $return['data']['refund_count'] -1; //退款记录数
                        $refund_no = $return['data']['out_refund_id_'.$refund_count] ?? $return['data']['out_refund_no_'.$refund_count]; //第三方退款单号or平台退款单号
                        $pay_time = isset($return['data']['refund_time_'.$refund_count]) ? date('Y-m-d H:i:s', strtotime($return['data']['refund_time_'.$refund_count])): ''; //退款时间,yyyyMMddHHmmss
                        $refund_amount = $return['data']['refund_fee_'.$refund_count] ? ($return['data']['refund_fee_'.$refund_count]/100): ''; //退款总金额,单位为分,可以做部分退款
                        $refund_amount = number_format($refund_amount, 2, '.', '');
                        $trade_no = $return['data']['out_transaction_id'] ?? $return['data']['refund_id_'.$refund_count]; //第三方订单号or平台退款单号

                        //改变数据库状态
                        if ($order->pay_status != 6) {
                            Cache::forget($c_o);

                            $insert_data = [
                                'status' => '6',
                                'pay_status' => 6,
                                'pay_status_desc' => '退款成功',
                                'refund_no' => $refund_no,
                                'refund_amount' => $refund_amount
                            ];
                            if ($pay_time) $insert_data['pay_time'] = $pay_time;
                            $this->update_day_order($insert_data, $out_trade_no, $table);

                            //退款成功后的动作
                            $return_data = [
                                'out_trade_no' => $out_trade_no,
                                'trade_no' => $trade_no,
                                'store_id' => $order->store_id,
                                'merchant_id' => $order->merchant_id,
                                'type' => $order->ways_type,
                                'ways_source' => $order->ways_source,
                                'refund_amount' => $refund_amount,
                                'refund_no' => $refund_no
                            ];
                            RefundOrder::query()->create($return_data);
                            RefundSuccessAction::action([
                                'out_trade_no'=>$out_trade_no,
                                'refund_no' => $refund_no, //联动流水号
                            ]);
                        }

                        return json_encode([
                            'status' => '1',
                            'pay_status' => 6,
                            'message' => '退款成功',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'pay_time' => $pay_time,
                                'out_trade_no' => $order->out_trade_no,
                                'refund_amount' => $refund_amount,
                                'total_amount' => $order->total_amount,
                                'other_no' => $other_no,
                                'trade_no' => $trade_no
                            ]
                        ]);
                    }
                    elseif($return['status'] == 2) {
                        return json_encode([
                            'status' => '2',
                            'pay_status' => '3',
                            'message' => '支付失败',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                    else {
                        return json_encode([
                            'status' => '1',
                            'pay_status' => 3,
                            'message' => $return['message'],
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                } else {
                    $return = $obj->order_query($hwcpay_data);

                    //支付成功
                    if ($return['status'] == 1) {
                        $pay_time = $return['data']['time_end'] ? date('Y-m-d H:i:s', strtotime($return['data']['time_end'])): ''; //支付完成时间，格式为yyyyMMddHHmmss，如2009年12月27日9点10分10秒表示为20091227091010
                        $trade_no = isset($return['data']['third_order_no']) ? $return['data']['third_order_no'] : $return['data']['transaction_id']; //平台交易号
                        $buyer_pay_amount = $return['data']['total_fee'] ? ($return['data']['total_fee']/100): ''; //总金额，以分为单位，不允许包含任何字、符号
                        $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                        $depBankSeq = $return['data']['out_transaction_id'] ?? ''; //第三方交易号

                        //改变数据库状态
                        if ($order->pay_status != 1) {
                            Cache::forget($c_o);

                            $insert_data = [
                                'status' => '1',
                                'pay_status' => 1,
                                'pay_status_desc' => '支付成功',
                                'buyer_logon_id' => '',
                                'trade_no' => $trade_no,
                                'pay_time' => $pay_time,
                                'buyer_pay_amount' => $buyer_pay_amount,
                                'qwx_no' => $depBankSeq
                            ];
                            $this->update_day_order($insert_data, $out_trade_no, $table);

                            //支付成功后的动作
                            $data = [
                                'ways_type' => $order->ways_type,
                                'company' => $order->company,
                                'ways_type_desc' => $order->ways_type_desc,
                                'source_type' => '28000', //返佣来源
                                'source_desc' => '汇旺财', //返佣来源说明
                                'total_amount' => $buyer_pay_amount,
                                'out_trade_no' => $order->out_trade_no,
                                'rate' => $order->rate,
                                'fee_amount' => $order->fee_amount,
                                'merchant_id' => $order->merchant_id,
                                'store_id' => $order->store_id,
                                'user_id' => $order->user_id,
                                'config_id' => $config_id,
                                'store_name' => $order->store_name,
                                'ways_source' => $order->ways_source,
                                'pay_time' => $pay_time,
                                'device_id' => isset($order->device_id) ? $order->device_id : "",
                            ];
                            PaySuccessAction::action($data);
                        }

                        return json_encode([
                            'status' => '1',
                            'pay_status' => 1,
                            'message' => '支付成功',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'pay_time' => $pay_time,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $buyer_pay_amount,
                                'total_amount' => $order->total_amount,
                                'other_no' => $other_no,
                                'trade_no' => $trade_no
                            ]
                        ]);
                    }
                    elseif($return['status'] == 2) {
                        return json_encode([
                            'status' => '2',
                            'pay_status' => '3',
                            'message' => '支付失败',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                    elseif($return['status'] == 8) {
                        return json_encode([
                            'status' => '1',
                            'pay_status' => '2', //1-成功 2-支付中
                            'message' => '用户支付中',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                    else {
                        return json_encode([
                            'status' => '1',
                            'pay_status' => 3,
                            'message' => $return['message'],
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                }

            }
            
            //银卡
            if (38000 < $ways_type && $ways_type < 38010) {
                $yinKaData = [
                    'out_trade_no' => $out_trade_no
                ];
                $return = (new \App\Api\Controllers\Yinka\PayController($store_id))->order_query($yinKaData);

                //支付成功
                if ($return['status'] == 1) {
                    $depBankSeq = '';
                    $trade_no = isset($return['data']['pcTrace']) ? $return['data']['pcTrace'] : ''; //不可空,银盛支付合作商户网站唯一订单号

                    $pay_time = date('Y-m-d H:i:s', time());
                    $buyer_pay_amount = $return['data']['tradeAmt'] * 0.01;
                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                            'other_no' => $depBankSeq
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '27000', //返佣来源
                            'source_desc' => '威富通', //返佣来源说明
                            'total_amount' => $buyer_pay_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => 1,
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                }
                elseif($return['status'] == 2) {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => '2', //1-成功 2-支付中
                        'message' => '用户支付中',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
                else {
                    return json_encode([
                        'status' => 2,
                        'pay_status' => 3,
                        'message' => $return['message'],
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }

            
            //兴业银行 交易查询
            if (39000 < $ways_type && $ways_type < 39010) {
                $config = new \App\Api\Controllers\Config\CibPayConfigController();
                $cibpay_config = $config->wftpay_config($config_id);
                if (!$cibpay_config) {
                    return json_encode([
                        'status' => '2',
                        'message' => '威富通支付配置不存在请检查配置'
                    ]);
                }

                $cibpay_merchant = $config->cib_merchant($store_id, $store_pid);
                if (!$cibpay_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '威富通商户号不存在'
                    ]);
                }

                //支付状态为 退款中、已退款、有退款，退款查询接口
                $obj = new WftPayPayController();
                $cibpay_data = [
                    'mch_id' => $cibpay_merchant->mch_id,
                    'out_trade_no' => $out_trade_no,
                    'private_rsa_key' => $cibpay_config->private_rsa_key,
                    'public_rsa_key' => $cibpay_config->public_rsa_key
                ];
                if ($order->pay_status == 5 || $order->pay_status == 6 || $order->pay_status == 7) {
                    $return = $obj->refund_query($cibpay_data); //0-系统错误 1-成功 2-退款失败 3-退款处理中 4-退款到银行发现用户的卡作废或者冻结了,导致原路退款银行卡失败

                    //支付成功
                    if ($return['status'] == 1) {
                        $refund_count = $return['data']['refund_count'] -1; //退款记录数
                        $refund_no = $return['data']['out_refund_id_'.$refund_count] ?? $return['data']['out_refund_no_'.$refund_count]; //第三方退款单号or平台退款单号
                        $pay_time = isset($return['data']['refund_time_'.$refund_count]) ? date('Y-m-d H:i:s', strtotime($return['data']['refund_time_'.$refund_count])): ''; //退款时间,yyyyMMddHHmmss
                        $refund_amount = isset($return['data']['refund_fee_'.$refund_count]) ? ($return['data']['refund_fee_'.$refund_count]/100): $order->total_amount; //退款总金额,单位为分,可以做部分退款
                        $refund_amount = number_format($refund_amount, 2, '.', '');
                        $trade_no = $return['data']['out_transaction_id']; //第三方订单号

                        //改变数据库状态
                        if ($order->pay_status != 6) {
                            Cache::forget($c_o);

                            $insert_data = [
                                'status' => '6',
                                'pay_status' => 6,
                                'pay_status_desc' => '退款成功',
                                'refund_no' => $refund_no,
                                'refund_amount' => $refund_amount
                            ];
                            if ($pay_time) $insert_data['pay_time'] = $pay_time;
                            $this->update_day_order($insert_data, $out_trade_no, $table);

                            //退款成功后的动作
                            $return_data = [
                                'out_trade_no' => $out_trade_no,
                                'trade_no' => $trade_no,
                                'store_id' => $order->store_id,
                                'merchant_id' => $order->merchant_id,
                                'type' => $order->ways_type,
                                'ways_source' => $order->ways_source,
                                'refund_amount' => $refund_amount,
                                'refund_no' => $refund_no
                            ];
                            RefundOrder::query()->create($return_data);
                            RefundSuccessAction::action([
                                'out_trade_no'=>$out_trade_no,
                                'refund_no' => $refund_no, //联动流水号
                            ]);
                        }

                        return json_encode([
                            'status' => '1',
                            'pay_status' => 6,
                            'message' => '退款成功',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'pay_time' => $pay_time,
                                'out_trade_no' => $order->out_trade_no,
                                'refund_amount' => $refund_amount,
                                'total_amount' => $order->total_amount,
                                'other_no' => $other_no,
                                'trade_no' => $trade_no
                            ]
                        ]);
                    }
                    elseif($return['status'] == 2) {
                        return json_encode([
                            'status' => '2',
                            'pay_status' => '2',
                            'message' => '支付中',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                    else {
                        return json_encode([
                            'status' => '1',
                            'pay_status' => 3,
                            'message' => $return['message'],
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                } else {

                    $return = $obj->order_query($cibpay_data); //0-系统错误 1-成功 2-失败 3-转入退款 4-未支付 5-已关闭 6-已冲正 7-已撤销 8-用户支付中
                   
                    //支付成功
                    if ($return['status'] == 1) {
                        $pay_time = $return['data']['time_end'] ? date('Y-m-d H:i:s', strtotime($return['data']['time_end'])): ''; //支付完成时间，格式为yyyyMMddHHmmss，如2009年12月27日9点10分10秒表示为20091227091010
                        $trade_no = $return['data']['transaction_id']; //平台交易号
                        $buyer_pay_amount = $return['data']['total_fee'] ? ($return['data']['total_fee']/100): ''; //总金额，以分为单位，不允许包含任何字、符号
                        $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                        $depBankSeq = $return['data']['out_transaction_id'] ?? ''; //第三方交易号

                        //改变数据库状态
                        if ($order->pay_status != 1) {
                            Cache::forget($c_o);

                            $insert_data = [
                                'status' => '1',
                                'pay_status' => 1,
                                'pay_status_desc' => '支付成功',
                                'buyer_logon_id' => '',
                                'trade_no' => $trade_no,
                                'pay_time' => $pay_time,
                                'buyer_pay_amount' => $buyer_pay_amount,
                                // 'other_no' => $depBankSeq
                            ];
                            $this->update_day_order($insert_data, $out_trade_no, $table);

                            //支付成功后的动作
                            $data = [
                                'ways_type' => $order->ways_type,
                                'company' => $order->company,
                                'ways_type_desc' => $order->ways_type_desc,
                                'source_type' => '27000', //返佣来源
                                'source_desc' => '威富通', //返佣来源说明
                                'total_amount' => $buyer_pay_amount,
                                'out_trade_no' => $order->out_trade_no,
                                'rate' => $order->rate,
                                'fee_amount' => $order->fee_amount,
                                'merchant_id' => $order->merchant_id,
                                'store_id' => $order->store_id,
                                'user_id' => $order->user_id,
                                'config_id' => $config_id,
                                'store_name' => $order->store_name,
                                'ways_source' => $order->ways_source,
                                'pay_time' => $pay_time,
                                'device_id' => isset($order->device_id) ? $order->device_id : "",
                            ];
                            PaySuccessAction::action($data);
                        }

                        return json_encode([
                            'status' => '1',
                            'pay_status' => 1,
                            'message' => '支付成功',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'pay_time' => $pay_time,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $buyer_pay_amount,
                                'total_amount' => $order->total_amount,
                                'other_no' => $other_no,
                                'trade_no' => $trade_no
                            ]
                        ]);
                    }
                    elseif($return['status'] == 2) {
                        return json_encode([
                            'status' => '2',
                            'pay_status' => '3',
                            'message' => '支付失败',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                    elseif($return['status'] == 8) {
                        return json_encode([
                            'status' => '1',
                            'pay_status' => '2', //1-成功 2-支付中
                            'message' => '用户支付中',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                    else {
                        return json_encode([
                            'status' => '1',
                            'pay_status' => 3,
                            'message' => $return['message'],
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount
                            ]
                        ]);
                    }
                }

            }

            //重庆银行 交易查询
            if (44000 < $ways_type && $ways_type < 44010) {
                return (new \App\Api\Controllers\Cqcbank\LogicController())->queryParams($store_id, $store_pid, $data, $order);
            }

            //民生银行 交易查询
            if (50001 == $ways_type || 50002 == $ways_type || 50003 == $ways_type) {
                return (new \App\Api\Controllers\Cmbc\LogicController())->queryParams($store_id, $store_pid, $data, $order);
            }

            //银盛
            //TODO:银盛订单查询
            if (in_array($ways_type, [14001, 14002, 14004])) {
                $manager = new \App\Api\Controllers\Config\YinshengConfigController();
                $yinsheng_config = $manager->yinsheng_config($data['config_id']);
                if (!$yinsheng_config) {
                    return json_encode([
                        'status' => '2',
                        'message' => '银盛支付不存在,请检查配置'
                    ]);
                }
                $yinsheng_merchant = $manager->yinsheng_merchant($store_id, $store_pid);
                if (!$yinsheng_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '银盛支付,商户号未配置'
                    ]);
                }
                $yinsheng_obj = new \App\Api\Controllers\YinSheng\PayController();
                $return = $yinsheng_obj->order_query([
                    'out_trade_no' => $out_trade_no,
                    'config_id' => $yinsheng_merchant->config_id,
                    'store_id' => $yinsheng_merchant->store_id,
                    'partner_id' => $yinsheng_merchant->partner_id,
                    'seller_id' => $yinsheng_merchant->seller_id,
                    'seller_name' => $yinsheng_merchant->seller_name,
                    'public_key' => $yinsheng_config->public_key,
                    'private_key' => $yinsheng_config->private_key,
                    'pfx_password' => $yinsheng_config->pfx_password,
                    'business_code' => $yinsheng_merchant->business_code
                ]);
                if ($return['status'] == 1) {
                    $pay_time = date('Y-m-d H:i:s', time());
                    $trade_no = $return['data']['trade_no'];
                    $buyer_pay_amount = $order->total_amount;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                            'other_no' => $other_no
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '14000', //返佣来源
                            'source_desc' => '银盛', //返佣来源说明
                            'total_amount' => $buyer_pay_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => 1,
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                }
                elseif($return['status'] == 2) {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => '2', //1-成功 2-支付中
                        'message' => '用户支付中',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
                else {
                    return json_encode([
                        'status' => '1',
                        'pay_status' => 3,
                        'message' => '支付错误',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }

            //东莞银行
            if (in_array($ways_type, [40001, 40002, 40003])) {
                $DongGuanConfig = (new \App\Api\Controllers\Config\Dong_GuanConfigController())->getConfig($store_id, $store_pid);
                if (!$DongGuanConfig) {
                    return json_encode([
                        'status' => '2',
                        'message' => '商户未配置东莞银行'
                    ]);
                }
                $channelResult = (new \App\Api\Controllers\Dong_Guan\PayController())->order_query([
                    'out_trade_no' => $out_trade_no,
                    'mch_id' => $DongGuanConfig['mch_id'],
                    'private_key' => $DongGuanConfig['private_key'],
                    'op_device_id' => $DongGuanConfig['op_device_id'],
                ]);
                //支付成功
                if ($channelResult['status'] == 1) {
                    $trade_no = $channelResult['data']['out_trade_no'];
                    $pay_time = $channelResult['data']['pay_time'];
                    $buyer_pay_amount = $channelResult['data']['total_amount'];;
                    //改变数据库状态
                    if ($order->pay_status == 2) {
                        Cache::forget($c_o);
                        $this->update_day_order([
                            'status' => 1,
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ], $out_trade_no);
                        //支付成功后的动作
                        PaySuccessAction::action([
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '40000', //返佣来源
                            'source_desc' => '东莞支付', //返佣来源说明
                            'total_amount' => $buyer_pay_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ]);
                    }
                    return json_encode([
                        'status' => '1',
                        'pay_status' => 1,
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'trade_no' => $trade_no
                        ]
                    ]);
                }
                //支付中
                if ($channelResult['status'] == 2) {
                    return json_encode([
                        'status' => 2,
                        'pay_status' => 2, //1-成功 2-支付中
                        'message' => '用户支付中',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
                //支付失败
                if ($channelResult['status'] == 3) {
                    return json_encode([
                        'status' => 3,
                        'pay_status' => 3,
                        'message' => $channelResult['message'],
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }

                return json_encode([
                    'status' => 2,
                    'pay_status' => 2,
                    'message' => '支付中',
                    'data' => [
                        'ways_source' => $order->ways_source,
                        'out_trade_no' => $order->out_trade_no,
                        'pay_amount' => $order->pay_amount,
                        'total_amount' => $order->total_amount
                    ]
                ]);
            }

            if (isset($ways_type)) {
                return (new \App\Api\Controllers\DevicePay\ScanpayController())->order_query($store_id, $store_pid, $data, $order);
            }


        } catch (\Exception $e) {
            $this->status = -1;
            $this->message = $e->getMessage() .' | '.$e->getFile().' | '. $e->getLine();
            return $this->format();
        }
    }
    
    //APP订单轮询-公共
    public function order_foreach_public_mini($data)
    {
        try {
            $store_id = $data['store_id'];
            $out_trade_no = isset($data['out_trade_no']) ? $data['out_trade_no'] : "";
            $other_no = isset($data['other_no']) ? $data['other_no'] : "";
            $table = isset($data['table']) ? $data['table'] : "";

            $ways_type = $data['ways_type'];
            $config_id = $data['config_id'];
            if ($out_trade_no) {
                $c_o = 'a' . $out_trade_no;
                $where[] = ['out_trade_no', '=', $out_trade_no];
            } elseif($other_no) {
                $c_o = 'a' . $other_no;
                $where[] = ['other_no', '=', $other_no];
            } else {
                $c_o = 'a' . $out_trade_no;
                $where[] = ['auth_code', '=', $out_trade_no];
            }

            if ($table == "") {
                $day = date('Ymd', time());
                $table = 'orders_' . $day;
            }
            $order = Cache::get($c_o);
            if (!$order) {
                if (Schema::hasTable($table)) {
                    $order = DB::table($table)->where('store_id', $store_id);
                } else {
                    $order = Order::where('store_id', $store_id);
                }
                //发起查询
                $order = $order->where($where)
                    ->select(
                        'id',
                        'ways_source',
                        'ways_type',
                        'company',
                        'ways_type_desc',
                        'pay_status',
                        'out_trade_no',
                        'trade_no',
                        'total_amount',
                        'pay_amount',
                        'qwx_no',
                        'rate',
                        'fee_amount',
                        'merchant_id',
                        'store_id',
                        'user_id',
                        'store_name',
                        'other_no',
                        'created_at'
                    )->first();

                Cache::put($c_o, $order, 1);
            }
            if (!$order) {
                return json_encode([
                    'status' => 2,
                    'message' => '订单号不存在'
                ]);
            }
            $ways_type = $order->ways_type;
            $out_trade_no = $order->out_trade_no;
            $other_no = $order->other_no;
            $created_at = $order->created_at;

            $store = Store::where('store_id', $store_id)
                ->select('config_id', 'merchant_id', 'pid')
                ->first();

            if (!$store) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店ID不存在'
                ]);
            }
            $config_id = $store->config_id;
            $store_pid = $store->pid;
            //工行收银支付
            if (19999 < $ways_type && $ways_type < 20999) {
                $config = new LianfuConfigController();
                $h_merchant = $config->lianfu_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '工行商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\lianfu\PayController();
                $data['apikey'] = $h_merchant->apikey; //
                $data['signkey'] = $h_merchant->signkey; //
                $data['appid'] = $h_merchant->appid; //
                $data['out_trade_no'] = $out_trade_no; //
                $data['total_amount'] = $order->total_amount; //
                $return = $obj->order_query_mini($data);
                //支付成功
                if ($return["status"] == 1) {
                    $pay_time = date('Y-m-d H:i:s', time());
                    $trade_no = isset($return['data']['order_id'])?$return['data']['order_id']:$out_trade_no;
                    $tp_order_id = isset($return['data']['tp_order_id'])?$return['data']['tp_order_id']:"";
                    $buyer_pay_amount = $order->total_amount;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'qwx_no' => $tp_order_id,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount
                        ];
                        $this->update_day_order($insert_data, $out_trade_no, $table);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '20200',//返佣来源
                            'source_desc' => 'lianfu',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : ""
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                            'qwx_no' => $tp_order_id,
                            'trade_no' => $trade_no
                        ]
                    ]);
                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no
                        ]
                    ]);
                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);

                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);

                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount
                        ]
                    ]);
                }
            }

        } catch (\Exception $e) {
            $this->status = -1;
            $this->message = $e->getMessage() .' | '.$e->getFile().' | '. $e->getLine();
            return $this->format();
        }
    }


    //花呗订单轮询
    public function hb_order_foreach(Request $request)
    {
        try {
            Log::info('hb_order_foreach');
            Log::info($request->all());
            $store_id = $request->get('store_id', '');
            $out_trade_no = $request->get('out_trade_no', '');
            $check_data = [
                'store_id' => '门店ID',
                'out_trade_no' => '订单号',
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            $data = [
                'store_id' => $store_id,
                'out_trade_no' => $out_trade_no
            ];

            return $this->hb_order_foreach_public($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage() . $exception->getLine();
            return $this->format();
        }
    }


    //花呗订单轮询
    public function hb_order_foreach_public($data)
    {
        try {
            $store_id = $data['store_id'];
            $out_trade_no = $data['out_trade_no'];
            $check_data = [
                'store_id' => '门店ID',
                'out_trade_no' => '订单号',
            ];
            $check = $this->check_required($data, $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            $AlipayHbOrder = AlipayHbOrder::where('out_trade_no', $out_trade_no)
                ->where('store_id', $store_id)
                ->select('ways_type', 'total_amount', 'ways_source')
                ->first();
            if (!$AlipayHbOrder) {
                return json_encode([
                    'status' => 2,
                    'message' => '订单号不存在'
                ]);
            }

            $store = Store::where('store_id', $store_id)
                ->select('config_id', 'merchant_id', 'pid', 'store_short_name')
                ->first();

            if (!$store) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店ID不存在'
                ]);
            }

            $config_id = $store->config_id;
            $store_pid = $store->pid;
            $store_name = $store->store_short_name;

            $ways_type = $AlipayHbOrder->ways_type;
            $total_amount = $AlipayHbOrder->total_amount;

            //官方支付宝
            if (999 < $ways_type && $ways_type < 1999) {
                //配置
                $isvconfig = new AlipayIsvConfigController();
                $storeInfo = $isvconfig->alipay_auth_info($store_id, $store_pid);
                $config = $isvconfig->AlipayIsvConfig($config_id);
                if (!$storeInfo) {
                    return json_encode([
                        'status' => 2,
                        'message' => '门店ID未授权'
                    ]);
                }

                $app_auth_token = $storeInfo->app_auth_token;

                $notify_url = url('/api/alipayopen/qr_pay_notify');
                $aop = new AopClient();
                $aop->apiVersion = "2.0";
                $aop->appId = $config->app_id;
                $aop->rsaPrivateKey = $config->rsa_private_key;
                $aop->alipayrsaPublicKey = $config->alipay_rsa_public_key;
                $aop->notify_url = $notify_url;
                $aop->signType = "RSA2"; //升级算法
                $aop->gatewayUrl = $config->alipay_gateway;
                $aop->format = "json";
                $aop->charset = "GBK";
                $aop->version = "2.0";
                $aop->method = 'alipay.trade.query';
                $requests = new AlipayTradeQueryRequest();
                $requests->setBizContent("{" .
                    "    \"out_trade_no\":\"" . $out_trade_no . "\"" .
                    "  }");
                $status = $aop->execute($requests, '', $app_auth_token);
                $resultCode = $status->alipay_trade_query_response->code;

                //异常
                if ($resultCode == 40004) {
                    $is_dqr = substr($out_trade_no, 0, 5);
                    //动态码在没有付款会报交易不存在
                    if ($is_dqr == "fq_qr") {
                        return json_encode([
                            'status' => 1,
                            'pay_status' => '2',
                            'message' => '等待支付'
                        ]);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $status->alipay_trade_query_response->sub_msg
                    ]);
                }

                //支付成功
                if ($status->alipay_trade_query_response->trade_status == "TRADE_SUCCESS") {
                    //改变数据库状态
                    AlipayHbOrder::where('out_trade_no', $out_trade_no)->update(
                        [
                            'trade_no' => $status->alipay_trade_query_response->trade_no,
                            'buyer_logon_id' => $status->alipay_trade_query_response->buyer_user_id,
                            'pay_status_desc' => '支付成功',
                            'pay_status' => 1,
                        ]);
                    $pay_time = $status->alipay_trade_query_response->send_pay_date;

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'out_trade_no' => $out_trade_no,
                            'ways_type' => $AlipayHbOrder->ways_type,
                            'ways_source' => $AlipayHbOrder->ways_source,
                            'total_amount' => $total_amount,
                            'pay_amount' => $total_amount,
                            'store_id' => $store_id,
                            'store_name' => $store_name,
                            'config_id' => $config_id,
                            'pay_time' => $pay_time,
                            'ways_source_desc' => '花呗分期',
                        ]
                    ]);
                } //等待付款
                elseif ($status->alipay_trade_query_response->trade_status == "WAIT_BUYER_PAY") {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付'
                    ]);

                } //订单关闭
                elseif ($status->alipay_trade_query_response->trade_status == 'TRADE_CLOSED') {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单关闭'
                    ]);
                } else {
                    //其他情况
                    $message = $status->alipay_trade_query_response->sub_msg;
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message
                    ]);
                }
            }

            //直付通
            if (16000 < $ways_type && $ways_type < 16999) {
                $config_type = '03';
                //配置
                $isvconfig = new AlipayIsvConfigController();
                $config = $isvconfig->AlipayIsvConfig($config_id, $config_type);
                if (!$config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '直付通配置不存在'
                    ]);
                }

                //判断直付通
                $AlipayZftStore = $isvconfig->AlipayZftStore($store_id, $store_pid);
                if (!$AlipayZftStore) {
                    return json_encode([
                        'status' => 2,
                        'message' => '直付通门店不存在'
                    ]);
                }

                $aop = new AopClient();
                $aop->apiVersion = "2.0";
                $aop->appId = $config->app_id;
                $aop->rsaPrivateKey = $config->rsa_private_key;
                $aop->alipayrsaPublicKey = $config->alipay_rsa_public_key;
                $aop->signType = "RSA2"; //升级算法
                $aop->gatewayUrl = $config->alipay_gateway;
                $aop->format = "json";
                $aop->charset = "GBK";
                $aop->version = "2.0";
                $aop->method = 'alipay.trade.query';
                $requests = new AlipayTradeQueryRequest();
                $requests->setBizContent("{" .
                    "    \"out_trade_no\":\"" . $out_trade_no . "\"" .
                    "  }");
                $status = $aop->execute($requests, '', '');
                $resultCode = $status->alipay_trade_query_response->code;
                //异常
                if ($resultCode == 40004) {
                    $is_dqr = substr($out_trade_no, 0, 5);
                    //动态码在没有付款会报交易不存在
                    if ($is_dqr == "fq_qr") {
                        return json_encode([
                            'status' => 1,
                            'pay_status' => '2',
                            'message' => '等待支付'
                        ]);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $status->alipay_trade_query_response->sub_msg
                    ]);
                }
                //支付成功
                if ($status->alipay_trade_query_response->trade_status == "TRADE_SUCCESS") {
                    //改变数据库状态
                    AlipayHbOrder::where('out_trade_no', $out_trade_no)->update(
                        [
                            'trade_no' => $status->alipay_trade_query_response->trade_no,
                            'buyer_logon_id' => $status->alipay_trade_query_response->buyer_user_id,
                            'pay_status_desc' => '支付成功',
                            'pay_status' => 1,
                        ]);
                    $pay_time = $status->alipay_trade_query_response->send_pay_date;

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'out_trade_no' => $out_trade_no,
                            'ways_type' => $AlipayHbOrder->ways_type,
                            'ways_source' => $AlipayHbOrder->ways_source,
                            'total_amount' => $total_amount,
                            'pay_amount' => $total_amount,
                            'store_id' => $store_id,
                            'store_name' => $store_name,
                            'config_id' => $config_id,
                            'pay_time' => $pay_time,
                            'ways_source_desc' => '花呗分期',
                        ]
                    ]);

                } //等待付款
                elseif ($status->alipay_trade_query_response->trade_status == "WAIT_BUYER_PAY") {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付'
                    ]);

                } //订单关闭
                elseif ($status->alipay_trade_query_response->trade_status == 'TRADE_CLOSED') {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单关闭'
                    ]);
                } else {
                    //其他情况
                    $message = $status->alipay_trade_query_response->sub_msg;
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message
                    ]);
                }
            }

        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage() . $exception->getLine();
            return $this->format();
        }
    }


    //小程序首页的数据
    public function weixinapp_index_count(Request $request)
    {
        try {
            $merchant = $this->parseToken();
            $merchant_id = $merchant->merchant_id;
            $store_ids = [];
            $where = [];

            //收银员
            if ($merchant->merchant_type == 2) {
                $where[] = ['merchant_id', '=', $merchant_id];
            }
//              error_log(date('Y-m-d H:i:s')." 【小程序首页的数据】： ".json_encode([$merchant_id])."\n\n", 3, "/www/wwwlogs/wft/". 'commission_store'.date('Ymd').".log");


            $MerchantStore = MerchantStore::where('merchant_id', $merchant_id)->select('store_id')->get();
            if (!$MerchantStore->isEmpty()) {
                $store_ids = $MerchantStore->toArray();
            }

            //下面需要优化 读取动态的
            $time_start = date('Y-m-d 00:00:00', time());
            $time_end = date('Y-m-d H:i:s', time());
            $obj = DB::table('orders');

            $where[] = ['orders.created_at', '>=', $time_start];
            $where[] = ['orders.created_at', '<=', $time_end];
            $store= DB::table('stores')->where('store_id',$store_ids[0]['store_id'])->select('*')->first();
            $id=$store->id;
            $order_data = DB::table('orders')->join('stores', function ($join) {$join->on('orders.store_id', '=', 'stores.store_id');})->whereIn('orders.pay_status', [1, 6])->where($where)->where(function ($query) use($id) {$query->where('stores.id', $id)->orwhere('stores.pid',$id);return $query;})->select([DB::raw('sum(orders.total_amount) as total_amount'),DB::raw('count(orders.out_trade_no) as total'),DB::raw('sum(orders.rata_money) as rata_money')])->first();
            $store123=DB::table('stores')->where('rata_status','1')->where('store_id',$store_ids[0]['store_id'])->where('pid','4377')->first();
            if(!empty($store123)){
               $rata_fee = round(($order_data->total_amount/100)*0.25,2);
            }else{
                $rata_fee = $order_data->rata_money?$order_data->rata_money:0.00; //今日服务费
            }

            $day_order = $order_data->total_amount?$order_data->total_amount:0.00; //交易金额
            $day_order_count = $order_data->total;
            $banners=DB::table('banners')->where('user_id',1)->select('*')->get();
            $news=DB::table('notice_news')->where('user_id',1)->select('*')->limit(2)->get();
                
            $server_store=DB::table('server_stores')->where('store_id',$store_ids[0]['store_id'])->first();
            
            $order = '';
            
            if(empty($server_store)){
                $server_store['status']='1';
            }


            $data = [
                'day_order' => $day_order,
                'day_order_count' => $day_order_count,
                'order_list' => $order,
                'store_money'=>$store->store_money,
                'store'=>$store,
                'banners'=>$banners,
                'news'=>$news,
                'rata_fee'=>$rata_fee,
                'server_store'=>$server_store
            ];

            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage() . $exception->getLine();
            return $this->format();
        }
    }


    //APP订单撤销
    public function order_cancel_public($data)
    {
        try {
            $store_id = $data['store_id'];
            $out_trade_no = isset($data['out_trade_no']) ? $data['out_trade_no'] : "";
            $other_no = isset($data['other_no']) ? $data['other_no'] : "";
            $ways_type = $data['ways_type'];
            $config_id = $data['config_id'];
            if ($out_trade_no) {
                $c_o = 'a' . $out_trade_no;
                $where[] = ['out_trade_no', '=', $out_trade_no];
            } else {
                $c_o = 'a' . $other_no;
                $where[] = ['other_no', '=', $other_no];
            }

            $order = Cache::get($c_o);
            if (!$order) {
                //发起查询
                $order = Order::where('store_id', $store_id)
                    ->where($where)
                    ->select(
                        'id',
                        'ways_source',
                        'ways_type',
                        'company',
                        'ways_type_desc',
                        'pay_status',
                        'out_trade_no',
                        'trade_no',
                        'total_amount',
                        'pay_amount',
                        'qwx_no',
                        'rate',
                        'fee_amount',
                        'merchant_id',
                        'store_id',
                        'user_id',
                        'store_name',
                        'other_no'
                    )->first();

                Cache::put($c_o, $order, 1);
            }

            if (!$order) {
                return json_encode([
                    'status' => 2,
                    'message' => '订单号不存在'
                ]);
            }

            $ways_type = $order->ways_type;
            $out_trade_no = $order->out_trade_no;
            $other_no = $order->other_no;

            $store = Store::where('store_id', $store_id)
                ->select('config_id', 'merchant_id', 'pid')
                ->first();
            if (!$store) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店ID不存在'
                ]);
            }

            $config_id = $store->config_id;
            $store_pid = $store->pid;

            //支付宝官方订单
            if (999 < $ways_type && $ways_type < 1999) {
                //配置
                $isvconfig = new AlipayIsvConfigController();
                $storeInfo = $isvconfig->alipay_auth_info($store_id, $store_pid);
                $config = $isvconfig->AlipayIsvConfig($config_id);

                $app_auth_token = $storeInfo->app_auth_token;

                $notify_url = url('/api/alipayopen/qr_pay_notify');
                $aop = new AopClient();
                $aop->apiVersion = "2.0";
                $aop->appId = $config->app_id;
                $aop->rsaPrivateKey = $config->rsa_private_key;
                $aop->alipayrsaPublicKey = $config->alipay_rsa_public_key;
                $aop->notify_url = $notify_url;
                $aop->signType = "RSA2"; //升级算法
                $aop->gatewayUrl = $config->alipay_gateway;
                $aop->format = "json";
                $aop->charset = "GBK";
                $aop->version = "2.0";
                $aop->method = 'alipay.trade.query';
                $requests = new AlipayTradeQueryRequest();
                $requests->setBizContent("{" .
                    "    \"out_trade_no\":\"" . $out_trade_no . "\"" .
                    "  }");
                $status = $aop->execute($requests, '', $app_auth_token);

                if ($status->alipay_trade_query_response->code == 40004) {
                    $is_dqr = substr($out_trade_no, 0, 3);
                    $pay_status = "3";
                    $re_status = 2;
                    //动态码在没有付款会报交易不存在
                    if ($is_dqr == "DQR") {
                        $pay_status = "2";
                        $re_status = 1;
                        return json_encode([
                            'status' => $re_status,
                            'pay_status' => $pay_status,
                            'message' => $status->alipay_trade_query_response->sub_msg,
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount,
                                'other_no' => $other_no
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => $re_status,
                            'pay_status' => $pay_status,
                            'message' => $status->alipay_trade_query_response->sub_msg,
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount,
                            ]
                        ]);
                    }
                }
                //支付成功
                if ($status->alipay_trade_query_response->trade_status == "TRADE_SUCCESS") {
                    $buyer_pay_amount = $status->alipay_trade_query_response->buyer_pay_amount;
                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);
                        $insert_data = [
                            'status' => 'TRADE_SUCCESS',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => $status->alipay_trade_query_response->buyer_user_id,
                            'trade_no' => $status->alipay_trade_query_response->trade_no,
                            'pay_time' => $status->alipay_trade_query_response->send_pay_date,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '1000',//返佣来源
                            'source_desc' => '支付宝',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $status->alipay_trade_query_response->send_pay_date,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_time' => $status->alipay_trade_query_response->send_pay_date,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);

                } //等待付款
                elseif ($status->alipay_trade_query_response->trade_status == "WAIT_BUYER_PAY") {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);

                } //订单关闭
                elseif ($status->alipay_trade_query_response->trade_status == 'TRADE_CLOSED') {
                    $order->update([
                        'status' => '4',
                        'pay_status' => 4,
                        'pay_status_desc' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                    $order->save();

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);

                } else {
                    //其他情况
                    $message = $status->alipay_trade_query_response->sub_msg;
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                }
            }

            //官方微信支付订单
            if (1999 < $ways_type && $ways_type < 2999) {
                $config = new WeixinConfigController();
                $options = $config->weixin_config($config_id);
                $weixin_store = $config->weixin_merchant($store_id, $store_pid);
                $wx_sub_merchant_id = $weixin_store->wx_sub_merchant_id;
                $config = [
                    'app_id' => $options['app_id'],
                    'mch_id' => $options['payment']['merchant_id'],
                    'key' => $options['payment']['key'],
                    'cert_path' => $options['payment']['cert_path'], // XXX: 绝对路径！！！！
                    'key_path' => $options['payment']['key_path'],     // XXX: 绝对路径！！！！
                    'sub_mch_id' => $wx_sub_merchant_id,
                    // 'device_info'     => '013467007045764',
                    // 'sub_app_id'      => '',
                    // ...
                ];

                $payment = Factory::payment($config);
                $query = $payment->order->queryByOutTradeNumber($order->out_trade_no);

                //成功
                if ($query['trade_state'] == 'SUCCESS') {
                    $re_data['result_code'] = 'SUCCESS';
                    $re_data['result_msg'] = '支付成功';
                    $re_data['outTradeNo'] = $order->qwx_no;
                    $re_data['out_transaction_id'] = $order->out_trade_no; //$query['transaction_id'];
                    $re_data['time_end'] = $query['time_end'];

                    if ($order->pay_status != 1) {
                        //改变数据库状态
                        Cache::forget($c_o);
                        $insert_data = [
                            'status' => 'TRADE_SUCCESS',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => $query['openid'],
                            'trade_no' => $query['transaction_id'],
                            'pay_time' => date('Y-m-d H:i:s', strtotime($query['time_end'])),
                        ];
                        $this->update_day_order($insert_data, $order->out_trade_no);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '2000',//返佣来源
                            'source_desc' => '微信支付',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => date('Y-m-d H:i:s', strtotime($query['time_end'])),
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => date('Y-m-d H:i:s', strtotime($query['time_end'])),
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);

                } elseif ($query['trade_state'] == "USERPAYING" || $query['trade_state'] == "NOTPAY") {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);
                } elseif ($query['trade_state'] == "CLOSED") {
                    $order->update([
                        'status' => '4',
                        'pay_status' => '4',
                        'pay_status_desc' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                    $order->save();

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                } elseif ($query['trade_state'] == "REVOKED") {
                    $order->update([
                        'status' => '3',
                        'pay_status' => '3',
                        'pay_status_desc' => '订单已撤销',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                    $order->save();

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $query['trade_state_desc'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                }
            }

            //官方微信a 支付订单
            if (3999 < $ways_type && $ways_type < 4999) {
                $config = new WeixinConfigController();
                $options = $config->weixina_config($config_id);
                $weixin_store = $config->weixina_merchant($store_id, $store_pid);
                $wx_sub_merchant_id = $weixin_store->wx_sub_merchant_id;
                $config = [
                    'app_id' => $options['app_id'],
                    'mch_id' => $options['payment']['merchant_id'],
                    'key' => $options['payment']['key'],
                    'cert_path' => $options['payment']['cert_path'], // XXX: 绝对路径！！！！
                    'key_path' => $options['payment']['key_path'],     // XXX: 绝对路径！！！！
                    'sub_mch_id' => $wx_sub_merchant_id,
                    // 'device_info'     => '013467007045764',
                    // 'sub_app_id'      => '',
                    // ...
                ];
                $payment = Factory::payment($config);
                $query = $payment->order->queryByOutTradeNumber($order->out_trade_no);

                //成功
                if ($query['trade_state'] == 'SUCCESS') {
                    $re_data['result_code'] = 'SUCCESS';
                    $re_data['result_msg'] = '支付成功';
                    $re_data['outTradeNo'] = $order->qwx_no;
                    $re_data['out_transaction_id'] = $order->out_trade_no; //$query['transaction_id'];
                    $re_data['time_end'] = $query['time_end'];

                    if ($order->pay_status != 1) {
                        //改变数据库状态
                        Cache::forget($c_o);
                        $insert_data = [
                            'status' => 'TRADE_SUCCESS',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => $query['openid'],
                            'trade_no' => $query['transaction_id'],
                            'pay_time' => date('Y-m-d H:i:s', strtotime($query['time_end'])),
                        ];
                        $this->update_day_order($insert_data, $order->out_trade_no);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '4000', //返佣来源
                            'source_desc' => '微信支付a', //返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => date('Y-m-d H:i:s', strtotime($query['time_end'])),
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => date('Y-m-d H:i:s', strtotime($query['time_end'])),
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);
                } elseif ($query['trade_state'] == "USERPAYING" || $query['trade_state'] == "NOTPAY") {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);
                } elseif ($query['trade_state'] == "CLOSED") {
                    $order->update([
                        'status' => '4',
                        'pay_status' => '4',
                        'pay_status_desc' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                    $order->save();

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                } elseif ($query['trade_state'] == "REVOKED") {
                    $order->update([
                        'status' => '3',
                        'pay_status' => '3',
                        'pay_status_desc' => '订单已撤销',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                    $order->save();

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单关闭',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $query['trade_state_desc'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                }
            }

            //京东收银支付
            if (5999 < $ways_type && $ways_type < 6999) {
                //读取配置
                $config = new JdConfigController();
                $jd_config = $config->jd_config($config_id);
                if (!$jd_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '京东配置不存在请检查配置'
                    ]);
                }

                $jd_merchant = $config->jd_merchant($store_id, $store_pid);
                if (!$jd_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '京东商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Jd\PayController();
                $data = [];
                $data['out_trade_no'] = $out_trade_no;
                $data['request_url'] = $obj->order_query_url; //请求地址;
                $data['merchant_no'] = $jd_merchant->merchant_no;
                $data['md_key'] = $jd_merchant->md_key; //
                $data['des_key'] = $jd_merchant->des_key; //
                $data['systemId'] = $jd_config->systemId; //
                $return = $obj->order_query($data);
                //支付成功
                if ($return["status"] == 1) {
                    $pay_time = date('Y-m-d H:i:s', strtotime($return['data']['payFinishTime']));
                    $trade_no = $return['data']['tradeNo'];
                    $channelNoSeq = isset($return['data']['channelNoSeq']) ? $return['data']['channelNoSeq'] : $trade_no; //条码
                    $buyer_pay_amount = $return['data']['piAmount'] / 100;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $channelNoSeq,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '6000',//返佣来源
                            'source_desc' => '京东金融',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);

                } //等待付款
                elseif ($return["status"] == 2) {

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);

                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                }
            }

            //快钱支付
            if (2999 < $ways_type && $ways_type < 3999) {
                //读取配置
                $MyBankobj = new MyBankConfigController();
                $MyBankConfig = $MyBankobj->MyBankConfig($config_id);
                if (!$MyBankConfig) {
                    return json_encode([
                        'status' => 2,
                        'message' => '快钱配置不存在请检查配置'
                    ]);
                }

                $mybank_merchant = $MyBankobj->mybank_merchant($store_id, $store_pid);
                if (!$mybank_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '快钱商户号不存在'
                    ]);
                }

                $MerchantId = $mybank_merchant->MerchantId;
                $obj = new TradePayController();
                $return = $obj->mybankOrderQuery($MerchantId, $config_id, $out_trade_no);
                if ($return['status'] == 0) {
                    return json_encode([
                        'status' => 2,
                        'message' => $return['message']
                    ]);
                }

                $body = $return['data']['document']['response']['body'];
                $TradeStatus = $body['TradeStatus'];

                //成功
                if ($TradeStatus == 'succ') {
                    $OrderNo = $body['MerchantOrderNo'];
                    $GmtPayment = $body['GmtPayment'];
                    $buyer_id = '';
                    if ($ways_type == 3004) {
                        $buyer_id = $body['SubOpenId'];
                    }
                    if ($ways_type == 3003) {
                        $buyer_id = $body['BuyerUserId'];
                    }

                    $pay_time = date('Y-m-d H:i:s', strtotime($GmtPayment));
                    $payment_method = strtolower($body['Credit']);

                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);
                        $insert_data = [
                            'status' => 1,
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_id' => $buyer_id,
                            'trade_no' => $OrderNo,
                            'pay_time' => $pay_time,
                            'payment_method' => $payment_method,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '3000',//返佣来源
                            'source_desc' => '快钱支付',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);

                } elseif ($TradeStatus == 'paying') {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = '请重新扫码';
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                }
            }

            //新大陆
            if (7999 < $ways_type && $ways_type < 8999) {
                //读取配置
                $config = new NewLandConfigController();
                $new_land_config = $config->new_land_config($config_id);
                if (!$new_land_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '新大陆配置不存在请检查配置'
                    ]);
                }

                $mybank_merchant = $config->new_land_merchant($store_id, $store_pid);
                if (!$mybank_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户新大陆通道未开通'
                    ]);
                }

                $request_data = [
                    'out_trade_no' => $out_trade_no,
                    'key' => $mybank_merchant->nl_key,
                    'org_no' => $new_land_config->org_no,
                    'merc_id' => $mybank_merchant->nl_mercId,
                    'trm_no' => $mybank_merchant->trmNo,
                    'op_sys' => '3',
                    'opr_id' => $store->merchant_id,
                    'trm_typ' => 'T',
                ];
                $obj = new PayController();
                $return = $obj->order_query($request_data);
                //支付成功
                if ($return["status"] == 1) {
                    $pay_time = date('Y-m-d H:i:s', strtotime($return['data']['sysTime']));
                    $trade_no = $return['data']['orderNo'];
                    $buyer_pay_amount = $return['data']['amount'] / 100;
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '8000',//返佣来源
                            'source_desc' => '新大陆',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);

                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);
                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                }
            }

            //和融通收银支付
            if (8999 < $ways_type && $ways_type < 9999) {
                //读取配置
                $config = new HConfigController();
                $h_config = $config->h_config($config_id);
                if (!$h_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '和融通不存在请检查配置'
                    ]);
                }

                $h_merchant = $config->h_merchant($store_id, $store_pid);
                if (!$h_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '和融通商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Huiyuanbao\PayController();
                $data = [];
                $data['out_trade_no'] = $out_trade_no;
                $data['request_url'] = $obj->order_query_url; //请求地址;
                $data['md_key'] = $h_config->md_key; //
                $data['mid'] = $h_merchant->h_mid; //
                $data['orgNo'] = $h_config->orgNo; //

                $return = $obj->order_query($data);

                //支付成功
                if ($return["status"] == 1) {
                    // $pay_time = strtotime($return['data']['timeEnd']) ? strtotime($return['data']['timeEnd']) : time();
                    $pay_time = date('Y-m-d H:i:s', time());
                    $trade_no = '112121' . $return['data']['transactionId'];
                    $buyer_pay_amount = $return['data']['totalFee'];
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);
                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '9000',//返佣来源
                            'source_desc' => '和融通',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);

                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);

                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);

                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);

                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);
                }
            }

            //ltf收银支付
            if (9999 < $ways_type && $ways_type < 10999) {
                //读取配置
                $config = new LtfConfigController();
                $ltf_merchant = $config->ltf_merchant($store_id, $store_pid);
                if (!$ltf_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Ltf\PayController();
                $data = [];
                $data['out_trade_no'] = $out_trade_no;
                $data['request_url'] = $obj->order_query_url; //请求地址;
                $data['merchant_no'] = $ltf_merchant->merchantCode;
                $data['appId'] = $ltf_merchant->appId; //
                $data['key'] = $ltf_merchant->md_key; //

                $return = $obj->order_query($data);
                //支付成功
                if ($return["status"] == 1) {
                    $pay_time = date('Y-m-d H:i:s', strtotime($return['data']['payTime']));
                    $trade_no = $return['data']['outTransactionId'];
                    $buyer_pay_amount = $return['data']['receiptAmount'];
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '10000',//返佣来源
                            'source_desc' => '联拓富',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);
                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'other_no' => $other_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);
                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);
                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);
                }
            }

            //随行付收银支付
            if (12999 < $ways_type && $ways_type < 13999) {
                //读取配置
                $config = new VbillConfigController();
                $vbill_config = $config->vbill_config($config_id);
                if (!$vbill_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '随行付配置不存在请检查配置'
                    ]);
                }

                $vbill_merchant = $config->vbill_merchant($store_id, $store_pid);
                if (!$vbill_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '随行付商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Vbill\PayController();
                $data['notify_url'] = url('/api/vbill/pay_notify_url'); //回调地址
                $data['request_url'] = $obj->order_query_url; //请求地址;
                $data['mno'] = $vbill_merchant->mno;
                $data['privateKey'] = $vbill_config->privateKey; //
                $data['sxfpublic'] = $vbill_config->sxfpublic; //
                $data['orgId'] = $vbill_config->orgId; //
                $data['out_trade_no'] = $order->out_trade_no;
                $return = $obj->order_query($data);
                //支付成功
                if ($return["status"] == 1) {
                    $pay_time = date('Y-m-d H:i:s', strtotime($return['data']['respData']['tranTime']));
                    $trade_no = $return['data']['respData']['sxfUuid'];
                    $buyer_pay_amount = $return['data']['respData']['oriTranAmt'];
                    $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);

                        $insert_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'buyer_logon_id' => '',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ];
                        $this->update_day_order($insert_data, $out_trade_no);

                        //支付成功后的动作
                        $data = [
                            'ways_type' => $order->ways_type,
                            'company' => $order->company,
                            'ways_type_desc' => $order->ways_type_desc,
                            'source_type' => '13000',//返佣来源
                            'source_desc' => '随行付',//返佣来源说明
                            'total_amount' => $order->total_amount,
                            'out_trade_no' => $order->out_trade_no,
                            'rate' => $order->rate,
                            'fee_amount' => $order->fee_amount,
                            'merchant_id' => $order->merchant_id,
                            'store_id' => $order->store_id,
                            'user_id' => $order->user_id,
                            'config_id' => $config_id,
                            'store_name' => $order->store_name,
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'device_id' => isset($order->device_id) ? $order->device_id : "",
                        ];
                        PaySuccessAction::action($data);
                    }

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '1',
                        'message' => '支付成功',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $pay_time,
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $buyer_pay_amount,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);

                } //等待付款
                elseif ($return["status"] == 2) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '2',
                        'message' => '等待支付',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->total_amount,
                            'total_amount' => $order->total_amount,
                        ]
                    ]);

                } //订单失败关闭
                elseif ($return["status"] == 3) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单支付失败',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);

                }//订单退款
                elseif ($return["status"] == 4) {
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => '订单已经退款',
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);

                } else {
                    //其他情况
                    $message = $return['message'];
                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);
                }
            }

            //哆啦宝
            if (14999 < $ways_type && $ways_type < 15999) {
                $manager = new ManageController();
                //读取配置
                $dlbconfig = $manager->pay_config($config_id);
                if (!$dlbconfig) {
                    return json_encode([
                        'status' => 2,
                        'message' => '哆啦宝配置配置不存在请检查配置'
                    ]);
                }

                $dlb_merchant = $manager->dlb_merchant($store_id, $store_pid);
                if (!$dlb_merchant && !empty($dlb_merchant->mch_num) && !empty($dlb_merchant->shop_num)) {
                    return json_encode([
                        'status' => 2,
                        'message' => '哆啦宝配置商户未补充商户编号等信息!'
                    ]);
                }

                $query_data = [
                    'agentNum' => $dlbconfig->agent_num,
                    'customerNum' => $dlb_merchant->mch_num,
                    'shopNum' => $dlb_merchant->shop_num,
                    'requestNum' => $order->out_trade_no,
                    'secretKey' => $dlbconfig->secret_key,
                    'accessKey' => $dlbconfig->access_key,
                ];
                $return = $manager->query_bill($query_data);
                if ($return['status'] == 0) {
                    return json_encode($return);
                }
                if ($return['status'] == 1) {
                    $query_result = $return['data'];
                    if ($query_result['status'] == "SUCCESS") {
                        $pay_time = $query_result['completeTime'];
                        $trade_no = $query_result['orderNum'];
                        $buyer_pay_amount = $query_result['orderAmount'];
                        //改变数据库状态
                        if ($order->pay_status != 1) {
                            Cache::forget($c_o);

                            $insert_data = [
                                'status' => '1',
                                'pay_status' => 1,
                                'pay_status_desc' => '支付成功',
                                'buyer_logon_id' => '',
                                'trade_no' => $trade_no,
                                'pay_time' => $pay_time,
                                'buyer_pay_amount' => $buyer_pay_amount,
                            ];
                            $this->update_day_order($insert_data, $out_trade_no);

                            //支付成功后的动作
                            $data = [
                                'ways_type' => $order->ways_type,
                                'ways_type_desc' => $order->ways_type_desc,
                                'source_type' => '15000',//返佣来源
                                'source_desc' => '哆啦宝',//返佣来源说明
                                'company' => 'dlb',//返佣来源说明
                                'total_amount' => $order->total_amount,
                                'out_trade_no' => $order->out_trade_no,
                                'rate' => $order->rate,
                                'merchant_id' => $order->merchant_id,
                                'store_id' => $order->store_id,
                                'user_id' => $order->user_id,
                                'config_id' => $config_id,
                                'store_name' => $order->store_name,
                                'ways_source' => $order->ways_source,
                                'pay_time' => $pay_time,
                                'device_id' => isset($order->device_id) ? $order->device_id : "",
                            ];
                            PaySuccessAction::action($data);
                        }

                        return json_encode([
                            'status' => 1,
                            'pay_status' => '1',
                            'message' => '支付成功',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'pay_time' => $pay_time,
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $buyer_pay_amount,
                                'total_amount' => $order->total_amount,
                                'other_no' => $other_no,
                            ]
                        ]);
                    } elseif ($query_result['status'] == "INIT") {
                        return json_encode([
                            'status' => 1,
                            'pay_status' => '2',
                            'message' => '等待支付',
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->total_amount,
                                'total_amount' => $order->total_amount,
                            ]
                        ]);
                    } elseif ($query_result['status'] == "CANCEL") {
                        return json_encode([
                            'status' => 1,
                            'pay_status' => '3',
                            'message' => '订单支付失败',
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount,
                            ],
                        ]);
                    } elseif ($query_result['status'] == "REFUND") {
                        return json_encode([
                            'status' => 1,
                            'pay_status' => '3',
                            'message' => '订单已经退款',
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount,
                            ],
                        ]);
                    } elseif ($query_result['status'] == "REFUNDING") {
                        return json_encode([
                            'status' => 1,
                            'pay_status' => '3',
                            'message' => '订单退款中',
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount,
                            ],
                        ]);
                    } elseif ($query_result['status'] == "FUNDFAIL") {
                        return json_encode([
                            'status' => 1,
                            'pay_status' => '3',
                            'message' => '订单退款失败',
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount,
                            ],
                        ]);
                    }
                }
            }

            //海科融通 交易关闭
            if (21999 < $ways_type && $ways_type < 22999) {
                //读取配置
                $config = new HkrtConfigController();
                $hkrt_config = $config->hkrt_config($config_id);
                if (!$hkrt_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '海科融通配置不存在请检查配置'
                    ]);
                }

                $hkrt_merchant = $config->hkrt_merchant($store_id, $store_pid);
                if (!$hkrt_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '海科融通商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Hkrt\PayController();
                $hkrt_order_close_data = [
                    'access_id' => $hkrt_config->access_id,
                    'trade_no' => $order->trade_no,
                    'out_trade_no' => $order->out_trade_no,
                    'access_key' => $hkrt_config->access_key
                ];
                Log::info('海科融通-商户-交易关闭');
                Log::info($hkrt_order_close_data);
                $return = $obj->order_close($hkrt_order_close_data); //0-失败 1-成功
                Log::info($return);

                if (isset($return) && !empty($return)) {
                    if ($return["status"] == 1) {
                        //改变数据库状态
                        if ($order->pay_status != 1) {
                            Cache::forget($c_o);
                            $insert_data = [
                                'status' => '4',
                                'pay_status' => '4',
                                'pay_status_desc' => '关闭',
                            ];
                            $this->update_day_order($insert_data, $out_trade_no);
                        }

                        return json_encode([
                            'status' => '1',
                            'pay_status' => '4',
                            'message' => '关闭',
                            'data' => [
                                'ways_source' => $order->ways_source,
                                'pay_time' => $order->pay_time,
                                'out_trade_no' => $out_trade_no,
                                'total_amount' => $order->total_amount,
                                'other_no' => $other_no,
                            ]
                        ]);
                    } else {
                        //其他情况
                        $message = $return['return_msg'];
                        return json_encode([
                            'status' => 1,
                            'pay_status' => '3',
                            'message' => $message,
                            'data' => [
                                'out_trade_no' => $order->out_trade_no,
                                'pay_amount' => $order->pay_amount,
                                'total_amount' => $order->total_amount,
                            ],
                        ]);
                    }
                } else {
                    return json_encode([
                        'status' => '0',
                        'message' => '系统错误'
                    ]);
                }
            }

            //易生支付 交易关闭
            if (in_array($ways_type,[21001,21002,21004,61001,61002,61004])) {
                //读取配置
                $version="easypay";
                if(in_array($ways_type,[61001,61002,61004])){
                    $version="easypaya";
                }
                $config = new EasyPayConfigController();
                $easypay_config = $config->easypay_config($config_id);
                if (!$easypay_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '易生支付配置不存在请检查配置'
                    ]);
                }

                $easypay_merchant = $config->easypay_merchant($store_id, $store_pid,$version);
                if (!$easypay_merchant) {
                    return json_encode([
                        'status' => 2,
                        'message' => '易生支付商户号不存在3'
                    ]);
                }

                $obj = new \App\Api\Controllers\EasyPay\PayController();
                $easypay_data = [
                    'channel_id' => $easypay_config->channel_id,
                    'mno' => $easypay_merchant->termMercode, //终端商戶编号
                    'device_id' => $easypay_merchant->termTermcode, //终端编号
                    'out_trade_no' => $order->out_trade_no,
                    'trade_no' => $order->trade_no
                ];
                Log::info('易生支付-商户-交易关闭');
                Log::info($easypay_data);
                $return = $obj->order_close($easypay_data); //-1 系统错误 0-其他 1-成功
                Log::info($return);

                if ($return['status'] == '1') {
                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);
                        $insert_data = [
                            'status' => '4',
                            'pay_status' => '4',
                            'pay_status_desc' => '关闭',
                        ];
                        $this->update_day_order($insert_data, $out_trade_no);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => '4',
                        'message' => '关闭',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $order->pay_time,
                            'out_trade_no' => $out_trade_no,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);
                }

            }

            //联动优势 交易关闭
            if (4999 < $ways_type && $ways_type < 5999) {
                $config = new LinkageConfigController();
                $linkage_config = $config->linkage_config($config_id);
                if (!$linkage_config) {
                    return json_encode([
                        'status' => '2',
                        'message' => '联动优势支付配置不存在请检查配置'
                    ]);
                }

                $linkage_merchant = $config->linkage_merchant($store_id, $store_pid);
                if (!$linkage_merchant) {
                    return json_encode([
                        'status' => '2',
                        'message' => '联动优势商户号不存在'
                    ]);
                }

                $obj = new \App\Api\Controllers\Linkage\PayController();
                $linkage_data = [
                    'acqSpId' => $linkage_config->mch_id, //代理商编号
                    'acqMerId' => $linkage_merchant->acqMerId, //商户号
                    'out_trade_no' => $order->out_trade_no, //订单号
                    'privateKey' => $linkage_config->privateKey, //
                    'publicKey' => $linkage_config->publicKey //
                ];
                Log::info('联动优势-商户-交易关闭');
                Log::info($linkage_data);
                $return = $obj->order_close($linkage_data); //-1系统错误；0-其他；1-交易成功；2-验签失败
                Log::info($return);

                if ($return['status'] == '1') {
                    //改变数据库状态
                    if ($order->pay_status != 1) {
                        Cache::forget($c_o);
                        $insert_data = [
                            'status' => '4',
                            'pay_status' => '4',
                            'pay_status_desc' => '关闭',
                        ];
                        $this->update_day_order($insert_data, $out_trade_no);
                    }

                    return json_encode([
                        'status' => '1',
                        'pay_status' => '4',
                        'message' => '关闭',
                        'data' => [
                            'ways_source' => $order->ways_source,
                            'pay_time' => $order->pay_time,
                            'out_trade_no' => $out_trade_no,
                            'total_amount' => $order->total_amount,
                            'other_no' => $other_no,
                        ]
                    ]);
                } else {
                    //其他情况
                    $message = $return['message'];

                    return json_encode([
                        'status' => 1,
                        'pay_status' => '3',
                        'message' => $message,
                        'data' => [
                            'out_trade_no' => $order->out_trade_no,
                            'pay_amount' => $order->pay_amount,
                            'total_amount' => $order->total_amount,
                        ],
                    ]);
                }

            }

        } catch (\Exception $exception) {
            Log::info('APP订单撤销-错误');
            Log::info($exception->getMessage().' | '.$exception->getFile().' | '.$exception->getLine());
            $this->status = -1;
            $this->message = $exception->getMessage().' | '.$exception->getFile().' | '.$exception->getLine();
            return $this->format();
        }
    }

    /**
     * 查看收银员
     * 2022-04-20 15:04
     */
    public function view_cashier(Request $request){
        // print_r( 2 );die;
        $curHdIndex = $request->get('curHdIndex', '');
        // $curtypeIndex = $request->get('curtypeIndex', '');
        $store_id = $request->get('store_id', '');

        $store = DB::table('stores')->where('store_id', $store_id)->select('store_id', 'store_name', 'id')->first(); // 查找总店
        $p_stores = DB::table('stores')->where('pid', $store->id)->select('store_id')->get(); // 查找总店下的分店
        if( !empty( $p_stores[0]->store_id ) ){ // 合并总店和分店
            $store_ids[0] = $store->store_id;
            $p_stores = json_decode($p_stores, true);
            foreach($p_stores as $k1 => $v1){
                $store_ids[] = $v1['store_id'];
            }
        }else{
            $store_ids[0] = $store->store_id;
        }
        $cashier = Db::table('merchant_stores')->where('merchant_id', '>', 0)->whereIn('store_id', $store_ids)->select('merchant_id', 'store_id')->get();
        $cashier = json_decode($cashier, true);
        // print_r( $curHdIndex );die;
        if($curHdIndex == 1){
            $where[] = ['created_at', '>=', date('Y-m-d') . ' 00:00:00'];
            $where[] = ['created_at', '<=', date('Y-m-d') . ' 23:59:59'];

            $total_amount = Order::whereIn('store_id', $store_ids)->where($where)->whereIn('pay_status', [1, 6])->select('total_amount')->sum('total_amount'); // 总收款
            $fee_amount = Order::whereIn('store_id', $store_ids)->where($where)->whereIn('pay_status', [1, 6])->select('fee_amount')->sum('fee_amount'); // 总手续费
            $refund_amount = Order::whereIn('store_id', $store_ids)->where($where)->whereIn('pay_status', [6])->select('refund_amount')->sum('refund_amount'); // 总退款
            $total_num = Order::whereIn('store_id', $store_ids)->where($where)->whereIn('pay_status', [1, 6])->select('total_amount')->count(); // 总收款笔数
            $refund_num = Order::whereIn('store_id', $store_ids)->where($where)->whereIn('pay_status', [6])->select('refund_amount')->count(); // 总退款笔数
            $final_amount = bcsub($total_amount, $fee_amount + $refund_amount, 2);
            $real_amount = bcsub($total_amount, $refund_amount, 2);
            $data = array();
            // print_r( $cashier );die;
            foreach($cashier as $key => $val){
                $cashier_data = DB::table('merchants')->where('id', $val['merchant_id'])->select('name')->first();
                $store = DB::table('stores')->where('store_id', $val['store_id'])->select('store_name','pid')->first();
                $data[$key]['merchant_id'] = $val['merchant_id'];
                $data[$key]['store_name'] = $store->store_name;
                $data[$key]['merchant_name'] = $cashier_data->name;
                // print_r( $val['store_id'] );die;
                $data[$key]['total_amount'] = DB::table('orders')->where('merchant_id', $val['merchant_id'])->where('store_id', $val['store_id'])->where($where)->whereIn('pay_status', [1, 6])->select()->sum('total_amount'); // 门店总金额
                $data[$key]['total_num'] = DB::table('orders')->where('merchant_id', $val['merchant_id'])->where('store_id', $val['store_id'])->where($where)->whereIn('pay_status', [1, 6])->select()->count('total_amount'); // 门店总笔数
                if($store->pid == 0 ){
                    $data[$key]['store_name'] = '总店：' . $store->store_name;
                }else{
                    $data[$key]['store_name'] = '分店：' . $store->store_name;
                }
                $data[$key]['total_num'] = '交易笔数：' . $data[$key]['total_num'] . '笔';
                $data[$key]['money_name'] = '入账金额';
                $data[$key]['store_id'] = $val['store_id'];
            }
            array_multisort( array_column($data, 'total_amount'), SORT_DESC, $data );
            // dump($data);die;
            $return = array('total_num' => $total_num, 'total_amount' => $total_amount, 'refund_num' => $refund_num, 'refund_amount' => $refund_amount, 'real_amount' => $real_amount, 'fee_amount' => $fee_amount, 'final_amount' => $final_amount, 'merchant_list' => $data);
            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($return);
        }
        if($curHdIndex == 2){
            $where[] = ['settling_day', '>=', date('Y-m-d', strtotime("-1 day"))];
            $where[] = ['settling_day', '<=', date('Y-m-d', strtotime("-1 day"))];
        }else if($curHdIndex == 3){
            $where[] = ['settling_day', '>=', date('Y-m-d', strtotime('last week'))];
            $where[] = ['settling_day', '<=', date('Y-m-d', strtotime('this week -1 day'))];
        }else if($curHdIndex == 4){
            $where[] = ['settling_day', '>=', date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00'))];
            $where[] = ['settling_day', '<=', date('Y-m-d', strtotime("-1 day"))];
        }else if($curHdIndex == 5){
            $where[] = ['settling_day', '>=', date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00')))];
            $where[] = ['settling_day', '<=', date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00') - 86400)];
        }else if($curHdIndex == 6){ // 
            $start_data = $request->get('start_data', '');
            $end_data = $request->get('end_data', '');
            
            $where[] = ['settling_day', '>=', $start_data];
            $where[] = ['settling_day', '<=', $end_data];
            // dump($where);die;
            if ($start_data != $end_data) {
                $date = ((strtotime($end_data) > strtotime($start_data)))?true:false;
                if (!$date) {
                    $this->status = '2';
                    $this->message = '开始时间不能超过结束时间';
                    return $this->format(2);
                }
            }
            // //限制时间
            $date = (strtotime($end_data) - strtotime($start_data)) / 86400;
            $day = 31;
            if ($date > $day) {
                $this->status = '2';
                $this->message = "时间筛选不能超过 $day 天";
                return $this->format(2);
            }
        }
        // dump( $curHdIndex );die;
        $store_amount = DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_amount')->sum('store_amount');
        $total_num = DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('store_order_count')->sum('store_order_count');
        $refund_amount = DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_amount')->sum('refund_amount');
        $refund_num = DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('refund_order_num')->sum('refund_order_num');
        $fee_amount = DB::table('stores_day_count')->whereIn('store_id', $store_ids)->where($where)->select('fee_amount')->sum('fee_amount');
        $final_amount = bcsub($store_amount, $fee_amount + $refund_amount, 2);
//        dump($refund_amount);die;
        $real_amount = bcsub($store_amount, $refund_amount, 2);
        // $data = [];
        foreach($cashier as $key =>$val){
            $cashier_data = DB::table('merchants')->where('id', $val['merchant_id'])->select('name')->first();
            $store = DB::table('stores')->where('store_id', $val['store_id'])->first();

            $data[$key]['merchant_id'] = $val['merchant_id'];
            $data[$key]['store_name'] = $store->store_name;
            $data[$key]['merchant_name'] = $cashier_data->name;
            $data[$key]['total_amount'] = DB::table('stores_cash_count')->where('store_id', $val['store_id'])->where($where)->where('merchant_id', $val['merchant_id'])->select('merchant_money')->sum('merchant_money'); // 门店总金额
            $data[$key]['total_num'] = DB::table('stores_cash_count')->where('store_id', $val['store_id'])->where($where)->where('merchant_id', $val['merchant_id'])->select('merchant_num')->sum('merchant_num'); // 门店总笔数
            if( $store->pid == 0 ){
                $data[$key]['store_name'] = '总店：' . $store->store_name;
            }else{
                $data[$key]['store_name'] = '分店：' . $store->store_name;
            }
            $data[$key]['total_num'] = '交易笔数：' . $data[$key]['total_num'] . '笔';
            $data[$key]['money_name'] = '入账金额';
            $data[$key]['store_id'] = $val['store_id'];
        }
        array_multisort( array_column($data, 'total_amount'), SORT_DESC, $data );

        $return = array('total_num' => $total_num, 'total_amount' => $store_amount, 'refund_num' => $refund_num, 'refund_amount' => $refund_amount, 'real_amount' => $real_amount, 'fee_amount' => $fee_amount, 'final_amount' => $final_amount, 'merchant_list' => $data);
        $this->status = 1;
        $this->message = '数据返回成功';
        return $this->format($return);
    }

    /**
     * 2022/08/24 16:32
     */
    function custom_query(Request $request){
        try{
            $merchant = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $merchant_id = $request->get('merchant_id', '');
            $time_start = $request->get( 'time_start', date('Y-m-d 00:00:00', time()) ); // 开始时间
            $time_end = $request->get( 'time_end', date('Y-m-d 23:59:59', time()) ); // 结束时间
            // print_r($time_end);die;
            // 限制时间
            $date = ( strtotime($time_end) - strtotime($time_start) ) / 86400;
            $day = 31;
            if($date > $day){
                return json_encode([
                    'status' => 2,
                    'message' => '时间跨度不能超过'.$day.'天'
                ]);
            }
            
            if( strtotime($time_end) > time() ){ // 结束时间超过今天改为今天
                // print_r( time() );die;
                $time_end = date('Y-m-d 23:59:59', time());
            }

            // 跨天操作
            $time_start_db = date('Ymd', strtotime($time_start));
            $time_end_db = date('Ymd', strtotime($time_end));;
            $is_ct_time = 0;
            if ($time_start_db != $time_end_db) {
                $is_ct_time = 1;
            }
//            print_r( $is_ct_time );die;
            $day = date('Ymd', strtotime($time_start));
            $day_end = date('Ymd', strtotime($time_end));
            $table = 'orders_' . $day;

            $where = [];
            if ($store_id) {
                $where[] = ['store_id', '=', $store_id];
                $store_ids = [
                    [
                        'store_id' => $store_id,
                    ]
                ];
            } else {
                $MerchantStore = MerchantStore::where('merchant_id', $merchant->merchant_id)->select('store_id')->get();
                if (!$MerchantStore->isEmpty()) {
                    $store_ids = $MerchantStore->toArray();
                    $where_stores = '';
                    foreach( $store_ids as $k => $v ){
                        if($k == 0){
                            $where_stores = "'" . $v['store_id'] . "'";
                        }else{
                            $where_stores = $where_stores . ", '" .$v['store_id']. "'";
                        }
                    }
                    $where_store = [ 'store_id', ' in ', '('.$where_stores.')' ];
//                    print_r( $store_ids );die;
                }
            }
//            print_r($store_ids);die;
            //是否传收银员ID
            if ($request->get('merchant_id', '')) {
                $where[] = ['merchant_id', '=', $request->get('merchant_id', '')];
            }

            if ($time_start) {
//                $time_start = date('Y-m-d 00:00:00', strtotime($time_start));
                $time_start = date('Y-m-d H:i:s', strtotime($time_start));
                $where[] = ['created_at', '>=', $time_start];
            }

            if ($time_end) {
//                $time_end = date('Y-m-d 23:59:59', strtotime($time_end));
                $time_end = date('Y-m-d H:i:s', strtotime($time_end));
                $where[] = ['created_at', '<=', $time_end];
            }
            //  $where[] = ['pay_status', '=', 1];
            if( $time_start >= date('Y-m-d 00:00:00', time()) ){ // 今天
                // print_r( $where );die;
                $obj6 = DB::table('orders');

                $obj6 = $obj6->where($where)->whereIn('pay_status', [1, 6])->whereIn('store_id', $store_ids)->orderBy('created_at', 'desc');
                $this->t = $obj6->count();
                $data['order_list'] = $this->page($obj6)->get();

                $data['total_amount'] = $obj6->sum('total_amount'); // 总金额
                $data['total_count'] = $obj6->count('total_amount'); // 总笔数
                $data['refund_count'] = $obj6->whereIn('pay_status', [6])->count('total_amount'); // 退款笔数
                $data['refund_amount'] = $obj6->whereIn('pay_status', [6])->sum('total_amount'); // 退款金额
//                $data['get_amount'] = $obj6->whereIn('pay_status', [1])->sum('total_amount'); // 实收金额
                $data['get_amount'] = $data['total_amount'] - $data['refund_amount']; // 实收金额
            }else{
                
                if( $is_ct_time ){
                    $test = '';
                    $time = $this->getDateFromRanges($day, $day_end);
                    $toEnd = count($time) - 1;
                    if (!$store_id) {
                        $where[] = $where_store;
                    }

                    // print_r( $time );die;
            //         foreach( $time as $key => $val ){
            //             $where = [];
            //             if ($store_id) {
            //                 $where[] = ['store_id', '=', $store_id];
            //             } else {
            //                 $MerchantStore = MerchantStore::where('merchant_id', $merchant->merchant_id)->select('store_id')->get();
            //                 if (!$MerchantStore->isEmpty()) {
            //                     $store_ids = $MerchantStore->toArray();
            //                     $where_stores = '';
            //                     foreach( $store_ids as $k => $v ){
            //                         if($k == 0){
            //                             $where_stores = "'" . $v['store_id'] . "'";
            //                         }else{
            //                             $where_stores = $where_stores . ", '" .$v['store_id']. "'";
            //                         }
            //                     }
            //                     $where_store = [ 'store_id', ' in ', '('.$where_stores.')' ];
            //                     // $where[] = $where_store;
            // //                    print_r( $store_ids );die;
            //                 }
            //             }
            //             // print_r($where_store);die;
            //             //是否传收银员ID
            //             if ($request->get('merchant_id', '')) {
            //                 $where[] = ['merchant_id', '=', $request->get('merchant_id', '')];
            //             }
            
            //             if ($time_start) {
            // //                $time_start = date('Y-m-d 00:00:00', strtotime($time_start));
            //                 $time_start = date('Y-m-d H:i:s', strtotime($time_start));
            //                 $where[] = ['created_at', '>=', $time_start];
            //             }
            
            //             if ($time_end) {
            // //                $time_end = date('Y-m-d 23:59:59', strtotime($time_end));
            //                 $time_end = date('Y-m-d H:i:s', strtotime($time_end));
            //                 $where[] = ['created_at', '<=', $time_end];
            //             }
            //             print_r( $val );die;
            //         }
            
                    // print_r( $where );die;
                    
                    
                    // print_r( $wheres );die;
                    foreach( $time as $key => $val ){
                        
                        $where = [];
                        if ($store_id) {
                            $where[] = ['store_id', '=', $store_id];
                            $store_ids = [
                                [
                                    'store_id' => $store_id,
                                ]
                            ];
                        } else {
                            $MerchantStore = MerchantStore::where('merchant_id', $merchant->merchant_id)->select('store_id')->get();
                            if (!$MerchantStore->isEmpty()) {
                                $store_ids = $MerchantStore->toArray();
                                $where_stores = '';
                                foreach( $store_ids as $k => $v ){
                                    if($k == 0){
                                        $where_stores = "'" . $v['store_id'] . "'";
                                    }else{
                                        $where_stores = $where_stores . ", '" .$v['store_id']. "'";
                                    }
                                }
                                $where_store = [ 'store_id', ' in ', '('.$where_stores.')' ];
            //                    print_r( $store_ids );die;
                            }
                        }
            //            print_r($store_ids);die;
                        //是否传收银员ID
                        if ($request->get('merchant_id', '')) {
                            $where[] = ['merchant_id', '=', $request->get('merchant_id', '')];
                        }
            
                        if ($time_start) {
                            if($key==0){
                                $where[] = ['created_at', '>=', $time_start];
                            }else{
                                $where[] = ['created_at', '>=', date('Y-m-d', strtotime($val)).' 00:00:00'];
                            }
                            // print_r($time_start);die;
            //                $time_start = date('Y-m-d 00:00:00', strtotime($time_start));
                            // $time_start = date('Y-m-d H:i:s', strtotime($time_start));
                            // $where[] = ['created_at', '>=', $time_start];
                        }
            
                        if ($time_end) {
                            // print_r($time_end);die;
                            if($key == $toEnd){
                                $where[] = ['created_at', '<=', $time_end];
                            }else{
                                $where[] = ['created_at', '<=', date('Y-m-d', strtotime($val)).' 23:59:59'];
                            }
            //                $time_end = date('Y-m-d 23:59:59', strtotime($time_end));
                            // $time_end = date('Y-m-d H:i:s', strtotime($time_end));
                            // $where[] = ['created_at', '<=', $time_end];
                        }
                        
                        if (!$store_id) {
                            $where[] = $where_store;
                        }
                        // print_r($where);die;
                        
                        $wheres = '';
                        foreach ($where as $k => $v){
    //                        print_r( $v[0] );die;
                            if( $v[0] == 'store_id' ){
                                if($k == 0){
                                    $wheres = $wheres.' where `'.$v[0].'`'.$v[1].$v[2];
                                }else{
                                    $wheres = $wheres.' and `'.$v[0].'`'.$v[1].$v[2];
                                }
                            }else{
                                if($k == 0){
                                    $wheres = $wheres.' where `'.$v[0].'`'.$v[1]."'".$v[2]."'";
                                }else{
                                    $wheres = $wheres.' and `'.$v[0].'`'.$v[1]."'".$v[2]."'";
                                }
                            }
                        }
                        
                        if( $val == date('Y-m-d', time()) ){ // 今天
                            $day_start = 'orders';
                        }else{
                            $day_start = 'orders_' . date('Ymd', strtotime($val));
                        }

                        if($key == 0){
                            $test = '(select * from `' . $day_start . '`' . $wheres . ' and ( `pay_status`=1 or `pay_status`=6 ) )'; // 列表数据
                            $count = '(select count(*) from `' . $day_start . '`' . $wheres . ' and ( `pay_status`=1 or `pay_status`=6 ) )'; // 总记录条数(分页)

                            $total_amount = '(select sum(total_amount) from `' . $day_start . '`' . $wheres . ' and ( `pay_status`=1 or `pay_status`=6 ) )'; // 总金额
                            $refund_amount = '(select sum(refund_amount) from `' . $day_start . '`' . $wheres . ' and `pay_status`=6)'; // 退款金额
                            $refund_count = '(select count(total_amount) from `' . $day_start . '`' . $wheres . ' and `pay_status`=6)'; // 退款笔数
                            $get_amount = '(select sum(total_amount) from `' . $day_start . '`' . $wheres . ' and `pay_status`=1)'; // 实收金额
                        }else{
                            $test = $test . ' union all ' . '(select * from `' . $day_start . '`' . $wheres . ' and ( `pay_status`=1 or `pay_status`=6 ) )'; // 列表数据
                            $count = $count . ' union all ' . '(select count(*) from `' . $day_start . '`' . $wheres . ' and ( `pay_status`=1 or `pay_status`=6 ) )'; // 总记录条数(分页)

                            $total_amount = $total_amount . ' union all ' . '(select sum(total_amount) from `' . $day_start . '`' . $wheres . ' and ( `pay_status`=1 or `pay_status`=6 ) )'; // 总金额
                            $refund_amount = $refund_amount . ' union all ' . '(select sum(refund_amount) from `' . $day_start . '`' . $wheres . ' and `pay_status`=6)'; // 退款金额
                            $refund_count = $refund_count . ' union all ' . '(select count(total_amount) from `' . $day_start . '`' . $wheres . ' and `pay_status`=6)'; // 退款笔数
                            $get_amount = $get_amount . ' union all ' . '(select sum(total_amount) from `' . $day_start . '`' . $wheres . ' and `pay_status`=1)'; // 实收金额
//                            print_r( $refund_amount );die;
                        }
                    }
//                    print_r( $refund_count );die;
                    $start = abs((abs(trim($request->get('p', 1))) - 1) * abs(trim($request->get('l', 25))));
                    $l = abs( trim($request->get('l', 25)) );
                    $test = $test . ' order by `created_at` desc limit ' . $start . "," . $l;
                    //->offset($start)->limit($this->l)

                    $datas = DB::select($test);
                    $count = DB::select($count);
                    $total_amount = DB::select($total_amount);
                    $refund_amount = DB::select($refund_amount);
                    $refund_count = DB::select($refund_count);
                    $get_amount = DB::select($get_amount);
                    // print_r( $test );die;


                    $datas = json_decode(json_encode($datas,true),true);
                    $count = json_decode( json_encode($count, true), true );
                    $total_amount = json_decode( json_encode($total_amount, true), true );
                    $refund_amount = json_decode( json_encode($refund_amount, true), true );
                    $refund_count = json_decode( json_encode($refund_count, true), true );
                    $get_amount = json_decode( json_encode($get_amount, true), true );
//                    print_r( $get_amount );die;


                    $counts = 0;
                    foreach($count as $k => $v){
                        $counts = $counts + $v['count(*)'];
                    }

                    $total_amounts = 0;
                    foreach($total_amount as $k => $v){
                        $total_amounts = $total_amounts + $v['sum(total_amount)'];
                    }
                    $total_amounts = number_format($total_amounts, 2, '.', '');

                    $refund_amounts = 0;
                    foreach($refund_amount as $k => $v){
                        $refund_amounts = $refund_amounts + $v['sum(refund_amount)'];
                    }
                    $refund_amounts = number_format($refund_amounts, 2, '.', '');

//                    print_r( $refund_count );die;
                    $refund_counts = 0;
                    foreach($refund_count as $k => $v){
                        $refund_counts = $refund_counts + $v['count(total_amount)'];
                    }

//                    print_r( 2 );die;
                    $get_amounts = 0;
                    foreach($get_amount as $k => $v){
                        $get_amounts = $get_amounts + $v['sum(total_amount)'];
                    }
//                    print_r( $get_amounts );die;


                    $this->status = 1;
                    $this->message = '数据返回成功';
                    foreach ($datas as $k => $v){
                        $datas[$k]['discount_amount'] = sprintf("%.2f",$v['discount_amount']);
//                            $datas[$k]['actual_amount'] = sprintf("%.2f",$v['total_amount'] - $v['discount_amount']);
                    }
                    $this->needpage = 1;
                    $this->t = $counts;

                    $data['order_list'] = $datas;
                    $data['total_amount'] = $total_amounts;

                    $data['total_count'] = $counts;
                    $data['refund_count'] = $refund_counts;
                    $data['refund_amount'] = $refund_amounts;
                    $data['get_amount'] = $get_amounts;
                }else{
                    // print_r(2);die;
                    $obj6 = DB::table($table)->where($where)->whereIn('pay_status', [1, 6])->whereIn('store_id', $store_ids)->orderBy('created_at', 'desc');
                    $this->t = $obj6->count();
                    $data['order_list'] = $this->page($obj6)->get();

                    $data['total_amount'] = $obj6->sum('total_amount'); // 总金额
                    $data['total_count'] = $obj6->count('total_amount'); // 总笔数
                    $data['refund_count'] = $obj6->whereIn('pay_status', [6])->count('total_amount'); // 退款笔数
                    $data['refund_amount'] = $obj6->whereIn('pay_status', [6])->sum('refund_amount'); // 退款金额
//                    $data['get_amount'] = $obj6->whereIn('pay_status', [1])->sum('total_amount'); // 实收金额
                    $data['get_amount'] = $data['total_amount'] - $data['refund_amount']; // 实收金额
                }
            }

            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);

        }catch(\Exception $exception){
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }
    
    function goods_count(Request $request){
        try {
            $export = $request->get('export', '');

            $merchant = $this->parseToken();

            $MerchantStore = MerchantStore::where('merchant_id', $merchant->merchant_id)->select('store_id')->get();
            $store_ids = $MerchantStore->toArray();

            $time_start = $request->get('time_start', '');
            $time_end = $request->get('time_end', '');
            $store_id = $request->get('store_id', '');

            if ($store_id) {
                $store_ids = [
                    [
                        'store_id' => $store_id,
                    ]
                ];
            } else {
                $MerchantStore = MerchantStore::where('merchant_id', $merchant->merchant_id)
                    ->select('store_id')
                    ->get();

                if (!$MerchantStore->isEmpty()) {
                    $store_ids = $MerchantStore->toArray();
                }
            }
            if( empty($time_start) || empty($time_end) ){
                $this->status = -1;
                $this->message = '请选择时间';
                return $this->format();
            }
            //限制时间
            $date = (strtotime($time_end) - strtotime($time_start)) / 86400;
            $day = 31;
            if ($date > $day) {
                return json_encode([
                    'status' => 2,
                    'message' => "时间筛选不能超过 $day 天"
                ]);
            }
            if( empty($time_end) ){
                $time_end = date('Y-m-d H:i:s', time());
            } else {
                if ($time_start != $time_end) {
                    $date = ((strtotime($time_end) > strtotime($time_start)))?true:false;
                    if (!$date) {
                        return json_encode([
                            'status' => 2,
                            'message' => '开始时间不能超过结束时间'
                        ]);
                    }
                }
            }
//            var_dump($wheres);

//            DB::connection()->enableQueryLog();
            $obj = DB::table('order_goods_details')->whereIn('store_id', $store_ids)
                ->where('created_at', '>=', $time_start)
                ->where('created_at', '<=', $time_end)
                ->where('status', '=', 1);

            if ($export == 1) {
                $dataArray = $this->page($obj)->select(
                    'store_id',
                    'store_name',
                    'out_trade_no',
                    'coupon_name',
                    'num',
                    'coupon_type',
                    'cost',
                    'company',
                    'created_at'
                )->get()->toArray();
                $filename = '单品对账导出.csv';
                $tileArray = ['门店ID', '门店名称', '订单号', '单品名称', '核销数量', '优惠类型', '服务费', '通道', '时间'];
                $dataArray = json_decode(json_encode($dataArray), true);

                return $this->exportToExcel($filename, $tileArray, $dataArray);
            } else {
                $this->t = $obj->count();
                $data = $this->page($obj)->get();
//            dd(DB::getQueryLog());
                if( !isset($data) ){
                    $this->status = -1;
                    $this->message = '暂无数据';
                    return $this->format();
                }
                return $this->format($data);
            }
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }

    public function exportToExcel($filename, $tileArray = [], $dataArray = [])
    {
        ini_set('memory_limit', '512M');
        ini_set('max_execution_time', 0);
        ob_end_clean();
        ob_start();
        header("Content-Type: text/csv");
        header("Content-Disposition:filename=" . $filename);
        $fp = fopen('php://output', 'w');
        fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));//转码 防止乱码(比如微信昵称(乱七八糟的))

//        fputcsv($fp, $s_array);
        fputcsv($fp, []);
        fputcsv($fp, $tileArray);
        $index = 0;
        foreach ($dataArray as $item) {
            if(!empty($item['out_trade_no'])){
                $item['out_trade_no'] = $item['out_trade_no']. "\t";
            }
            if(!empty($item['store_id'])){
                $item['store_id'] = $item['store_id']. "\t";
            }
            $index++;
            fputcsv($fp, $item);
        }

        ob_flush();
        flush();
        ob_end_clean();
    }
}
